将身份从.net核心1.1升级到2.0

时间:2017-11-12 10:51:26

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

我有以下代码在1.1处工作。

 public static IServiceCollection RegisterRepositoryServices(this IServiceCollection services)
        {
            services.AddIdentity<ApplicationUser, IdentityRole<int>>(
                config => { config.User.RequireUniqueEmail = true;
                    config.Cookies.ApplicationCookie.LoginPath = "/Account/Login";
                    config.Cookies.ApplicationCookie.AuthenticationScheme = "Cookie";
                    config.Cookies.ApplicationCookie.AutomaticAuthenticate = false;
                    config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents()
                    {
                        OnRedirectToLogin = async ctx =>
                        {
                            if (ctx.Request.Path.StartsWithSegments("/visualjobs") && ctx.Response.StatusCode == 200)
                            {
                                ctx.Response.StatusCode = 401;
                            }
                            else
                            {
                                ctx.Response.Redirect(ctx.RedirectUri);
                            }
                            await Task.Yield();
                        }
                    };
                }).AddEntityFrameworkStores<VisualJobsDbContext, int>()
              .AddDefaultTokenProviders();

            services.AddEntityFrameworkSqlServer().AddDbContext<VisualJobsDbContext>();

            services.AddScoped<IRecruiterRepository, RecruiterRepository>();
            services.AddSingleton<IAccountRepository, AccountRepository>();

            return services;
        }

它现在不喜欢引用config.Cookies ....

的部分

我一直在网上搜索,但我真的找不到任何东西来取代它。

1 个答案:

答案 0 :(得分:0)

您需要致电AddIdentity添加Cookie身份验证服务,然后使用ConfigureApplicationCookie配置设置,如下所示:

services.AddIdentity<ApplicationUser, IdentityRole<int>>(config => {
        config.User.RequireUniqueEmail = true;
    })
.AddUserStore<UserStore<ApplicationUser, IdentityRole<int>, VisualJobsDbContext, int>>()
.AddDefaultTokenProviders();

services.ConfigureApplicationCookie(o => {
    o.LoginPath = new PathString("/Account/Login");
    o.Events = new CookieAuthenticationEvents()
    {
        OnRedirectToLogin = async ctx =>
        {
            if (ctx.Request.Path.StartsWithSegments("/visualjobs") && ctx.Response.StatusCode == 200)
            {
                ctx.Response.StatusCode = 401;
            }
            else
            {
                ctx.Response.Redirect(ctx.RedirectUri);
            }
            await Task.Yield();
        }
    };
});

此外,在您的Configure()方法中,请记得致电以下内容:

app.UseAuthentication();

补充阅读: Migrating Authentication and Identity to ASP.NET Core 2.0

注意: 本文的以下部分涉及AutomaticAuthenticate选项:

  

设置默认身份验证方案

     

在1.x中,AutomaticAuthenticateAutomaticChallenge   意图AuthenticationOptions基类的属性   设置在单一身份验证方案上。没有好办法   强制执行此操作。在2.0中,这两个属性已被删除   单个AuthenticationOptions实例上的属性。他们   可以在AddAuthentication方法调用中配置   Startup.cs的ConfigureServices方法:

     

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);