2

我有一个 ASP.NET MVC 应用程序,我正在尝试将 DotNetOpenAuth 用于我的 Google OAuth。我正在使用示例中的 GoogleConsumer 类,并尝试执行身份验证的第一步。下面的代码与提供的 WebForms 应用程序中的代码基本相同,只是在 MVC 控制器中:

public string Authenticate()
{
  GoogleTokenManager tokenManager = new GoogleTokenManager(ConsumerKey, ConsumerSecret);
  WebConsumer webConsumer = new WebConsumer(GoogleConsumer.ServiceDescription, tokenManager);
  GoogleConsumer.RequestAuthorization(webConsumer, GoogleConsumer.Applications.Gmail);
  return "";
}

当我向控制器发出 AJAX 请求时,代码会执行,但我从未被重定向到 Google 页面进行身份验证。

4

1 回答 1

3

底层请求返回了一个我没有正确处理的 302 重定向响应。我发现更有帮助的是在我的控制器中指定另一个操作的回调 URL,如下所示:

public ActionResult Authenticate()
{
  string callbackUrl = Request.Url.ToString().Replace("Authenticate", "OtherAction");
  Uri callback = new Uri(callbackUrl);

  WebConsumer webConsumer = new WebConsumer(GoogleConsumer.ServiceDescription, TokenManager);
  Dictionary<string, string> extraParameters = new Dictionary<string, string>();
  extraParameters.Add("scope", GoogleConsumer.GetScopeUri(GoogleConsumer.Applications.Gmail));

  UserAuthorizationRequest request = webConsumer.PrepareRequestUserAuthorization(callback, extraParameters, null);
  return webConsumer.Channel.PrepareResponse(request).AsActionResult();
}

public ActionResult OtherAction()
{
  // oauth_verifier, oauth_token are now in the RequestQueryString
}
于 2011-04-26T01:28:53.853 回答