簡體   English   中英

UseCookieAuthentication與UseExternalSignInCookie

[英]UseCookieAuthentication vs. UseExternalSignInCookie

我使用Owin通過Google oAuth進行授權。 以下是我的cookie配置方式:

// Enable the application to use a cookie to store information for the signed in user
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Authentication/Login")
});
// Use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

所以我正在使用UseCookieAuthentication和UseExternalSignInCookie,這似乎是多余的。 我應該為IAuthenticationManager方法(SignIn,SingOUt等)指定這兩種AuthenticationType中的哪一種? 或者我應該只保留其中一個?

更新。 讓我最困惑的是SignIn方法:

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

所以來自ExternalCookie的注銷,但在ApplicationCookie中有跡象。

如果您希望Google登錄工作,則需要所有這些內容。 這是它的工作原理。 在OWIN管道中,您有三個中間件組件:(1)以活動模式運行的cookie身份驗證中間件,(2)另一個cookie身份驗證中間件實例,但以被動模式運行,以及(3)Google身份驗證中間件。 那將是這樣的。

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    ...
}); // Active

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); // Passive

app.UseGoogleAuthentication(...);

當有401時,您的用戶會被重定向到Google。 在那里,您的用戶登錄,Google會驗證憑據。 然后,Google會將用戶重定向回您的應用。 此時,Google身份驗證中間件獲取登錄信息,應用授權(讀取外部cookie)並將OWIN管道短路並重定向到外部回調URL,這對應於AccountController ExternalLoginCallback操作方法。 因此,此時,當請求因重定向而進入您的應用時,您將獲得包含用戶名和電子郵件聲明的外部cookie。

要讀取此cookie並從Google檢索數據(用戶名等),您需要使用以被動模式運行的cookie身份驗證中間件。 由於此中間件以被動模式運行,因此必須告知它讀取cookie。 這就是在ExternalLoginCallback操作方法中調用AuthenticationManager.GetExternalLoginInfoAsync()時發生的情況。 此時,已建立外部cookie的身份,此身份僅包含來自Google的姓名和電子郵件聲明。

通常,此時您需要從應用程序數據存儲中檢索用戶特定信息,並向身份添加更多聲明。 因此,您在外部cookie中間件上調用Signout ,這也將確保外部cookie不再通過使其過期而被回送。 因此,使用當時可用的身份信息,在ExternalLoginCallback操作方法中調用UserManager.FindAsync ,該方法應返回具有所有特定於應用程序的聲明的用戶。 使用該新標識,您可以在以活動模式運行的cookie身份驗證中間件上調用SignIn 這可確保創建新的cookie。 與外部cookie相比,此新cookie包含所有特定於應用程序的聲明。 隨后,您將獲得此cookie,並且以活動模式運行的cookie身份驗證中間件會主動讀取cookie並使用所有特定於應用程序的聲明的完整列表建立標識。

因此,如果您不調用Signin,則不會創建包含所有特定於應用程序的聲明的cookie。 但是你需要使用其他機制。 開箱即用的行為是通過對SignIn調用創建包含所有特定於應用程序的聲明的本地cookie,然后由在活動模式下運行的cookie中間件讀取。

更新:我已經創建了一篇博客文章來解釋如何在不使用兩個cookie中間件實例的情況下逃脫。 http://lbadri.wordpress.com/2014/10/14/barebones-asp-net-mvc-google-signin-through-owin-middleware/

“SignOut(DefaultAuthenticationTypes.ExternalCookie)”是根據Hao Kung的回答“清理,外部cookie” https://stackoverflow.com/a/20575643/2710179

Microsoft.aspnet.identity.samples項目中有一個很好的實現,您可以從nuget下載。 在這個實現中,他們使用: -

    var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

“ExternalCookie”是“用於配置ExternalSignInAuthenticationType的默認值”我認為這意味着它被用作臨時cookie用於驗證用戶是否對外部視線

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM