C# 谷歌混合OpenID+;带有dotnetopenauth的OAuth

C# 谷歌混合OpenID+;带有dotnetopenauth的OAuth,c#,dotnetopenauth,google-openid,google-oauth,C#,Dotnetopenauth,Google Openid,Google Oauth,在过去的两天里,我大概花了10多个小时试图理解如何使用Google混合OpenID+OAuth()实现用户登录 要触发授权请求,我使用: InMemoryOAuthTokenManager tm = new InMemoryOAuthTokenManager( ConfigurationManager.AppSettings["googleConsumerKey"], ConfigurationManager.AppSettings["googleConsumerSecret"]); using

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

要触发授权请求,我使用:

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();
}
要检索我使用的accesstoken,请执行以下操作:

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;
    }
  }
}
使用(OpenIdRelyingParty openid=new OpenIdRelyingParty())
{
IAAuthenticationResponse authResponse=openid.GetResponse();
if(authResponse!=null)
{
开关(authResponse.Status)
{
案例验证状态。已验证:
HttpContext.Current.Trace.Write(“AuthenticationStatus”、“Authenticated”);
FetchResponse fr=authResponse.GetExtension();
InMemoryAuthTokenManager tm=新的InMemoryAuthTokenManager(ConfigurationManager.AppSettings[“googleConsumerKey”]、ConfigurationManager.AppSettings[“GoogleConsumerCret”]);
ServiceProviderDescription spd=新ServiceProviderDescription{
spd.RequestTokenEndpoint=新的DotNetOpenAuth.Messaging.MessageReceivingEndpoint(“https://accounts.google.com/o/oauth2/token,HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest);
spd.AccessTokenEndpoint=新的DotNetOpenAuth.Messaging.MessageReceivingEndpoint(“https://accounts.google.com/o/oauth2/token,HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest);
spd.UserAuthorizationEndpoint=新的DotNetOpenAuth.Messaging.MessageReceivingEndpoint(“https://accounts.google.com/o/oauth2/auth?access_type=offline,HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest);
spd.TamperProtectionElements=新的ITamperProtectionChannelBindingElement[]{new-HmacSha1SigningBindingElement()};
网络消费者wc=新的网络消费者(spd,tm);
AuthorizedTokenResponse accessToken=wc.ProcessUserAuthorization();
if(accessToken!=null)
{
HttpContext.Current.Trace.Write(“accessToken”,accessToken.ToString());
}
其他的
{
}
打破
案例身份验证状态。已取消:
HttpContext.Current.Trace.Write(“AuthenticationStatus”、“Cancelled”);
打破
案例身份验证状态。失败:
HttpContext.Current.Trace.Write(“AuthenticationStatus”、“Failed”);
打破
违约:
打破
}
}
}
不幸的是,我得到了
AuthenticationStatus.Authenticated
但是
wc.ProcessUserAuthorization()
null

我做错了什么


非常感谢您的帮助。

不要使用
WebConsumer
,而是使用NuGet软件包中提供的
WebConsumerOpenIdRelyingParty
类。该类提供了将OAuth请求作为OpenID扩展附加的帮助方法(您自己做得很好)以及在返回时提取OpenID扩展响应

查看可能有助于激发您的灵感。在DotNetOpenAuth中还有一个专门的Google OpenID登录和OAuth扩展示例,然后查看OpenIdRelyingPartyWebForms示例项目的loginPlusOAuth.aspx页面(以及代码隐藏和支持类)