Asp.net-Identity

AspNetCore - 使用 Google 身份驗證時更改 cookie 名稱

  • October 22, 2019

在 ASP.NET 5、MVC 6 中,我能夠在選項中更改外部身份驗證 cookie 的名稱——但這似乎已從AspNetCore.Identity RC2庫中的新提供程序中刪除。

我有這個設置;

class Startup {
  ...
  public void ConfigureServices(IServiceCollection services){
     services.AddIdentity<Member, Role> ... // identity wired up
  }

  public void Configure(IApplicationBuilder app, ILoggerFactory logger) {
     // .. other wiring
   app
       .UseIdentity()
       .UseGoogleAuthentication
       (new GoogleOptions {
           ClientId = Constants.Google.Client,
           ClientSecret = Constants.Google.Secret,
           Scope = {"email", "profile"}
       });

   app.UseMvc(routes => {
       routes.MapRoute(
           name: "default",
           template: "{controller=Home}/{action=Index}/{id?}");
        });
   }
}

曾經有一個AuthenticationType屬性可以設置為 a string,它可以控制 cookie 名稱;但那已經過去了。

我讀了其他文章說要嘗試SignInScheme並且AuthenticationScheme- 我做到了,但這會開始給我一個錯誤,即存在No Provider to Handle this Scheme.

有什麼我可以做的嗎?

下面介紹如何替換用於外部 cookie 的預設名稱。

services.AddIdentity<Member, Role>(options =>
{
   options.Cookies.ExternalCookie.CookieName = "name";
});

它在 VS2017 中對我有用

在 Startup.cs ConfigureServices() 中:

services.ConfigureApplicationCookie(options => {
 options.Cookie.Name = "NewCookieName";
});

引用自:https://stackoverflow.com/questions/36562344