我試圖調試與OWIN和GoogleAuthentication的問題,一切工作重定向到谷歌,然後谷歌重定向回來,我的自定義GoogleAuthProvider被調用,然後最終重新發生回到所需的頁面。爲什麼GoogleAuthentication後User.Identity.IsAuthenticated不正確?
不幸的是,在重定向回到所需的頁面後,!User.Identity.IsAuthenticated
是false所以我的帳戶控制器假定它需要重定向回谷歌進行身份驗證。 我的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);
}
,請參考文章:[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
我認爲這是對窗體身份驗證,我不確定它適用於此,因爲我作爲示例使用的示例不使用FormsAuthentication.SetAuthCookie。https://github.com/tjoudeh/AngularJSAuthentication –
migh t幫助https://github.com/google/google-api-dotnet-client-samples/tree/master/Calendar.ASP.NET.MVC5 – DaImTo