【问题标题】:Google Hybrid OpenID+OAuth with dotnetopenauth带有 dotnetopenauth 的 Google 混合 OpenID+OAuth
【发布时间】:2012-10-18 22:57:21
【问题描述】:

在过去的两天里,我花了大约 10 多个小时试图了解如何使用 Google Hybrid OpenID+OAuth (Federated Login) 实现用户登录

触发我使用的授权请求:

InMemoryOAuthTokenManager tm = new InMemoryOAuthTokenManager( ConfigurationManager.AppSettings["googleConsumerKey"], ConfigurationManager.AppSettings["googleConsumerSecret"]);
using (OpenIdRelyingParty openid = new OpenIdRelyingParty())
{
  Realm realm = HttpContext.Current.Request.Url.Scheme + Uri.SchemeDelimiter + ConfigurationManager.AppSettings["googleConsumerKey"] + "/";
  IAuthenticationRequest request = openid.CreateRequest(identifier, Realm.AutoDetect, new Uri(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + "/OAuth/google"));

  var authorizationRequest = new AuthorizationRequest
  {
    Consumer = ConfigurationManager.AppSettings["googleConsumerKey"],
    Scope = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.me",
  };

  request.AddExtension(authorizationRequest);

  request.AddExtension(new ClaimsRequest
  {
    Email = DemandLevel.Request,
    Gender = DemandLevel.Require
  });

  request.RedirectToProvider();
}

检索我使用的访问令牌:

using (OpenIdRelyingParty openid = new OpenIdRelyingParty())
{
  IAuthenticationResponse authResponse = openid.GetResponse();
  if (authResponse != null)
  {
    switch (authResponse.Status)
    {
      case AuthenticationStatus.Authenticated:
        HttpContext.Current.Trace.Write("AuthenticationStatus", "Authenticated");
        FetchResponse fr = authResponse.GetExtension<FetchResponse>();

        InMemoryOAuthTokenManager tm = new InMemoryOAuthTokenManager(ConfigurationManager.AppSettings["googleConsumerKey"], ConfigurationManager.AppSettings["googleConsumerSecret"]);

        ServiceProviderDescription spd = new ServiceProviderDescription {
          spd.RequestTokenEndpoint = new DotNetOpenAuth.Messaging.MessageReceivingEndpoint("https://accounts.google.com/o/oauth2/token", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest);
          spd.AccessTokenEndpoint = new DotNetOpenAuth.Messaging.MessageReceivingEndpoint("https://accounts.google.com/o/oauth2/token", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest);
          spd.UserAuthorizationEndpoint = new DotNetOpenAuth.Messaging.MessageReceivingEndpoint("https://accounts.google.com/o/oauth2/auth?access_type=offline", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest);
          spd.TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() };

        WebConsumer wc = new WebConsumer(spd, tm);
        AuthorizedTokenResponse accessToken = wc.ProcessUserAuthorization();

        if (accessToken != null)
        {
          HttpContext.Current.Trace.Write("accessToken", accessToken.ToString());
        }
        else
        {
        }
        break;
      case AuthenticationStatus.Canceled:
        HttpContext.Current.Trace.Write("AuthenticationStatus", "Canceled");
        break;
      case AuthenticationStatus.Failed:
        HttpContext.Current.Trace.Write("AuthenticationStatus", "Failed");
        break;
      default:
        break;
    }
  }
}

不幸的是,我得到了AuthenticationStatus.Authenticated,但wc.ProcessUserAuthorization()null

我做错了什么?

非常感谢您的帮助。

【问题讨论】:

    标签: c# dotnetopenauth google-openid google-oauth


    【解决方案1】:

    不要使用WebConsumer,而是使用WebConsumerOpenIdRelyingParty 类,该类在DotNetOpenAuth.OpenIdOAuth NuGet 包中提供。此类提供了辅助方法,用于将 OAuth 请求附加为 OpenID 扩展(无论如何您自己都做得很好)并在返回的路上提取 OpenID 扩展响应。

    查看the source code for the above mentioned class 可能有助于激发您的灵感。 DotNetOpenAuth 中还有一个专门针对 Google OpenID 登录和 OAuth 扩展的示例。 Get the samples from SourceForge,然后查看 OpenIdRelyingPartyWebForms 示例项目的 loginPlusOAuth.aspx 页面(以及代码隐藏和支持类)。

    【讨论】:

      猜你喜欢
      • 2011-03-02
      • 2011-10-13
      • 2014-02-20
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 2013-05-15
      • 1970-01-01
      相关资源
      最近更新 更多