UseCookieAuthentication与UseExternalSignInCookie

我使用Owin通过Google oAuth进行授权。 以下是我的cookie如何configuration:

// 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的迹象。

如果您希望Googlelogin工作,则需要所有这些信息。 这是如何工作的。 在OWINpipe道中,您有三个中间件组件:(1)以主动模式运行的Cookie身份validation中间件,(2)另一个Cookie身份validation中间件实例,但以被动模式运行,以及(3)Google身份validation中间件。 那将是如此。

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

如果有401,您的用户将被redirect到Google。 在那里,您的用户login并且Googlevalidation凭证。 Google然后将用户redirect回您的应用程序。 此时,Google身份validation中间件获取login信息,应用授权(读取外部cookie)并将OWINpipe道短路,并redirect到与AccountController ExternalLoginCallback操作方法对应的外部callbackURL。 所以,在这个时候,当请求来到您的应用程序作为redirect的结果,你得到的用户名和电子邮件声明的外部cookie。

为了读取此cookie并从Google中检索数据(用户名等),您需要使用以被动模式运行的cookie身份validation中间件。 由于这个中间件运行在被动模式,所以必须告诉读取cookie。 这是在ExternalLoginCallback操作方法中调用AuthenticationManager.GetExternalLoginInfoAsync()时发生的情况。 此时,来自外部Cookie的身份已经build立,并且此身份仅包含来自Google的姓名和电子邮件声明。

通常,在这一点上,您将需要从您的应用程序数据存储中检索用户特定的信息,并向该身份添加更多的声明。 因此,您Signout在外部Cookie中间件上调用Signout ,这也将确保外部Cookie不会因为过期而被送回。 因此,使用当时可用的身份信息, UserManager.FindAsyncExternalLoginCallback操作方法中被调用,该方法应该返回用户所有应用程序特定的声明。 使用该新身份,您可以在运行在活动模式下的cookie身份validation中间件上调用SignIn 。 这确保了一个新的cookie被创build。 与外部cookie相比,这个新的cookie包含所有应用程序特定的声明。 随后,您将返回此Cookie,并且以主动模式运行的Cookie身份validation中间件主动读取Cookie,并build立具有所有应用程序特定声明的完整列表的身份。

所以,如果您不打电话给Signin,那么您将不会创build包含所有应用程序特定索赔的Cookie。 但是,这取决于你使用其他机制。 开箱即用的行为是,通过对SignIn调用创build包含所有特定于应用程序的声明的本地cookie,然后通过以主动模式运行的cookie中间件读取。

更新:我已经创build了一篇博客文章,解释如何不使用两个cookie中间件实例。 http://lbadri.wordpress.com/2014/10/14/barebones-asp-net-mvc-google-signin-through-owin-middleware/

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

Microsoft.aspnet.identity.samples项目中有一个很好的实现,您可以从nuget下载。 在这个实现中,他们使用:

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

“ExternalCookie”是“configuration的ExternalSignInAuthenticationType使用的默认值”我相信这意味着它被用作临时Cookie使用来validation用户对外部视线