DotNetOpenAuth WebConsumer 未重定向到 ASP.NET MVC 应用程序中的 OAuth 提供程序

发布于 2024-11-03 14:07:52 字数 561 浏览 4 评论 0原文

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

I have an ASP.NET MVC app in which I am trying to have use DotNetOpenAuth for my Google OAuth. I am using the GoogleConsumer class from the sample, and attempting to do the first step of the authentication. The code below is essentially the same as that in the provided WebForms application, just in an MVC controller:

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

The code executes when I make an AJAX request to the controller, but I am never redirected to the Google page for authentication.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染墨丶若流云 2024-11-10 14:07:52

底层请求返回了 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
}

The underlying request was returning a 302 redirect response which I wasn't handling properly. What I found to be more helpful was to specify the callback URL to another action in my controller, as follows:

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文