2015-08-18 51 views
1

我試圖調試與OWIN和GoogleAuthentication的問題,一切工作重定向到谷歌,然後谷歌重定向回來,我的自定義GoogleAuthProvider被調用,然後最終重新發生回到所需的頁面。爲什麼GoogleAuthentication後User.Identity.IsAuthenticated不正確?

不幸的是,在重定向回到所需的頁面後,!User.Identity.IsAuthenticatedfalse所以我的帳戶控制器假定它需要重定向回谷歌進行身份驗證。 我的GoogleAuthProvider.Authenticated(GoogleOAuth2AuthenticatedContext context)函數被調用,並且如果在重定向之前顯示當前用戶已被認證。

什麼會導致用戶在重定向時變得未經身份驗證? 爲什麼不是應用程序設置或讀取請求之間的身份驗證值?

這裏是我的GoogleAuthProvider

public class GoogleAuthProvider : IGoogleOAuth2AuthenticationProvider 
    { 
     public void ApplyRedirect(GoogleOAuth2ApplyRedirectContext context) 
     { 
      context.Response.Redirect(context.RedirectUri); 
     } 

     public Task Authenticated(GoogleOAuth2AuthenticatedContext context) 
     { 
      context.Identity.AddClaim(new Claim("ExternalAccessToken", context.AccessToken)); 
      return Task.FromResult<object>(null); 
     } 

     public Task ReturnEndpoint(GoogleOAuth2ReturnEndpointContext context) 
     { 
      return Task.FromResult<object>(null); 
     } 
    } 

這裏是我的ConfigureOAuth

public void ConfigureOAuth(IAppBuilder app) 
     { 
      app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 
      OAuthBearerOptions = new OAuthBearerAuthenticationOptions(); 

      // Token Generation 
      app.UseOAuthAuthorizationServer(OAuthOptions); 
      app.UseOAuthBearerAuthentication(OAuthBearerOptions); 

      //Configure Google External Login 
      GoogleAuthOptions = new GoogleOAuth2AuthenticationOptions 
      { 
       ClientId = "x", 
       ClientSecret = "x", 
       Provider = new GoogleAuthProvider() 
      }; 
      app.UseGoogleAuthentication(GoogleAuthOptions); 
} 
+0

,請參考文章:[http://stackoverflow.com/questions/14508495/user-identity-isauthenticated-returns-false-after-setting-cookie-and-validating] [1] [1]:http://stackoverflow.com/questions/14508495/user-identity-isauthenticated-returns-false-after-setting-cookie-and-validating – Mostafa

+0

我認爲這是對窗體身份驗證,我不確定它適用於此,因爲我作爲示例使用的示例不使用FormsAuthentication.SetAuthCookie。https://github.com/tjoudeh/AngularJSAuthentication –

+0

migh t幫助https://github.com/google/google-api-dotnet-client-samples/tree/master/Calendar.ASP.NET.MVC5 – DaImTo

回答

1

你所有的代碼似乎不錯 但是,你所要做的,是讓 「Google+的API。」 你可以從這裏 http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

得到更多的幫助如何啓用 在開發者控制檯dashboardlook「啓用API」。點擊並在搜索類型中使用Google+ API

您可以在此處啓用。

希望這有助於enter image description here

+0

我不記得我做了什麼來解決它,但你的答案是徹底和有益的,所以我會選擇它,直到有人有一個很好的反駁。 –