【发布时间】:2017-11-09 00:30:34
【问题描述】:
我正在尝试在我的 ASP.NET Core 2.0 项目中使用 Identity 实现身份验证,但它似乎不起作用。我实现 Identity 的最后一个项目是 ASP.NET MVC 5 项目,情况发生了很大变化。我跳过了 Core 1.0 和 1.1,所以我不知道它是如何在这些下完成的,尽管从我所读的内容来看,它应该大多相似。
但我无法让它为我工作。当我说它不起作用时,我的意思是即使我没有被授权,我也没有被重定向到登录页面。
我所做的唯一定制是实现我自己的用户存储和我自己的扩展来添加身份而不需要角色,因为我不会使用角色。我可以对我搞砸的事情使用一些方向,因为从我的角度来看,现在一切都太复杂了。这是我目前得到的代码:
Startup.cs
public void ConfigureServices(
IServiceCollection services) {
services.AddDbContext<CustomDbContext>();
services.AddTransient<IUserStore<GlobalUser>, CustomUserStore<GlobalUser>>();
services.AddIdentity<GlobalUser>();
services.AddMvc();
}
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
} else {
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc();
}
ServiceCollectionExtensions.cs
我只是查看了内置 AddIdentity<TUser, TRole> 的源代码并省略了与角色相关的内容,所以这里应该没有问题,尽管可能......
public static class ServiceCollectionExtensions {
public static IdentityBuilder AddIdentity<TUser>(
this IServiceCollection services,
Action<IdentityOptions> optionsAction = null)
where TUser : class {
services.AddAuthentication(
o => {
o.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
o.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
o.DefaultSignInScheme = IdentityConstants.ApplicationScheme;
}).AddCookie(
o => {
o.LoginPath = new PathString("/Login");
o.Events = new CookieAuthenticationEvents {
OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
};
});
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
services.TryAddScoped<IdentityErrorDescriber>();
services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser>>();
services.TryAddScoped<UserManager<TUser>, AspNetUserManager<TUser>>();
services.TryAddScoped<SignInManager<TUser>, SignInManager<TUser>>();
if (optionsAction != null) {
services.Configure(optionsAction);
}
return new IdentityBuilder(typeof(TUser), services);
}
}
我以为我必须像在 MVC 5 中一样添加授权过滤器,但我似乎无法在全局范围内这样做。当我将它应用到默认控制器时,我得到以下异常:
没有指定 authenticationScheme,也没有 找到了 DefaultChallengeScheme。
但我以为我是在自定义 AddIdentity 方法中设置方案?我可以使用一些指导,我很感激任何被发送到我的方式。
【问题讨论】:
-
您是否将方案作为控制器中 Authorize 属性的一部分传入?像这样:
[Authorize(AuthenticationSchemes = IdentityConstants.ApplicationScheme)],或者类似的东西?我不知道IdentityConstants.ApplicationScheme是不是你需要的枚举。 -
所以,我按照您的建议将“Identity.Application”硬编码为方案,现在异常更改为
No authentication handler is configured to authenticate for the scheme: Identity.Application。我很难接受这些计划。告诉 ASP.NET 我想要使用 Identity 进行 cookie 身份验证的正确方法是什么?
标签: c# asp.net asp.net-core asp.net-identity asp.net-core-identity