Dan*_*tos 7 c# asp.net-mvc asp.net-identity asp.net-core
我正在研究基于角色授权的遗留项目,但我遇到了一些问题. User.IsInRole("admin")并[Authorize(Roles = "admin")]始终未通过授权.将User.IsInRole()始终返回False.我很确定用户已正确添加到角色中.Role name 'admin' is already taken. User already in role 'admin'.
也许一些服务正在影响另一个.
这是我的startup.cs恢复代码:
public void ConfigureServices(IServiceCollection services){
services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(connetctionString));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, CustomUserClaimsPrincipalFactory>();
services.AddMvc();
services.AddDistributedMemoryCache();
services.AddSession();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env){
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes => {...});
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
PS.是的,我记录了你并登录.
PS.是的,用户是管理员角色
PS."admin"以小写形式正确
PS.ApplicationDbContext继承IdentityDbContext
PS2.这是我的数据
SELECT id,username FROM aspnetusers;
|id | username |
|c4f7bf16... | admin@admin.com |
SELECT Id,Name FROM aspnetroles;
|Id | Name |
|50e2a572... | admin |
SELECT * FROM aspnetuserroles;
|UserId | RoleId |
|c4f7bf16... | 50e2a572...|
Run Code Online (Sandbox Code Playgroud)
在经历了很多错误之后,我终于找到了我所缺少的东西。
我只延长了UserClaimsPrincipalFactory<ApplicationUser>不UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>。
正确扩展角色后即可声明结束。
public class MyUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1496 次 |
| 最近记录: |