2012-10-18 2 views
3

와 나는 오픈 ID + OAuth는 (Federated Login)구글 하이브리드 + OAuth를 dotnetopenauth

권한 부여 요청 I를 트리거하려면 구글 하이브리드와 사용자 로그인을 구현하는 방법을 이해하려고 노력 지난 이틀 동안 아마 10 시간 이상을 보냈습니다 오픈 ID 사용

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; 
    } 
    } 
} 

는 Unfortunatelly 나는 AuthenticationStatus.Authenticated하지만를 얻을 수은 null입니다.

내가 뭘 잘못하고 있니?

도움을 주셔서 감사합니다.

답변

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 페이지 (코드 숨김 및 지원 클래스)를 살펴보십시오.