AspNetCore - 使用Google身份验证时更改Cookie名称

时间:2016-04-12 02:02:09

标签: asp.net-identity asp.net-core

在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属性我可以设置为string,它将控制cookie名称;但那已经消失了。

我阅读了其他帖子,据说尝试SignInSchemeAuthenticationScheme - 而且我做了,但这会让我误以为No Provider to Handle this Scheme

我有什么可以做的吗?

3 个答案:

答案 0 :(得分:5)

以下是如何替换用于外部cookie的默认名称。

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

答案 1 :(得分:2)

在VS2017中它对我有用

在Startup.cs ConfigureServices()中:

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

答案 2 :(得分:0)

.net core 2.0

    services.AddAuthentication(options => { options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; })
        .AddCookie(options =>
        {
            options.Cookie.Name = "my_cookie";
        })