[英]upgrading identity from .net core 1.1 to 2.0
我有下面的代碼在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的部分。
我一直在搜索網絡,但是我真的找不到任何東西可以替代它。
您需要調用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();
其他閱讀:將身份驗證和身份遷移到ASP.NET Core 2.0
注意:本文的以下部分與AutomaticAuthenticate
選項有關:
設置默認身份驗證方案
在1.x中, AuthenticationOptions基類的
AutomaticAuthenticate
和AutomaticChallenge
屬性旨在在單個身份驗證方案上進行設置。 沒有強制執行此操作的好方法。 在2.0中,這兩個屬性已作為單獨的AuthenticationOptions
實例上的屬性刪除。 可以在Startup.cs的ConfigureServices
方法內的AddAuthentication
方法調用中對其進行ConfigureServices
:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.