【问题标题】:DotNetOpenAuth WebConsumer not redirecting to OAuth provider in ASP.NET MVC appDotNetOpenAuth WebConsumer 未重定向到 ASP.NET MVC 应用程序中的 OAuth 提供程序
【发布时间】:2011-08-12 15:48:59
【问题描述】:

我有一个 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 页面进行身份验证。

【问题讨论】:

    标签: asp.net-mvc dotnetopenauth google-authentication


    【解决方案1】:

    基础请求返回 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
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-09
      • 2015-06-29
      • 2022-01-23
      • 2012-12-06
      • 2015-05-18
      • 1970-01-01
      • 2017-12-12
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多