最近,我能够为应用成功实现身份验证(使用WS-Federation的ADFS进行SSO)。现在,我试图理解并获得授权,所以这个问题可能不清楚。
我正在使用this topic来通过用于Identity的自定义存储提供程序来实现角色,而没有实体框架。
我已经设置了自定义的User
和Role
模型,以及实现适当接口的自定义UserStore
和RoleStore
。还有可供使用的角色表。
[尝试访问[Authorized]
或[Authorized(Roles = "RoleName")]
时遇到问题。如预期的那样,这些操作要求我向ADFS进行身份验证,但是当我提交正确的凭据时,登录循环几次,并显示ADFS错误页面。没有角色实现就不会出现ADFS的此问题。 UserStore
和RoleStore
尚未实现代码,但应用程序从未尝试使用其任何方法。
我尝试在Startup.cs
中实现不同的选项,其中一些我已注释掉,并对服务进行了重新排序。将伪代码插入RoleStore
也无济于事。基本上,我只想能够使用Identity从自定义存储中添加role checks。在用户登录以找到其角色后,我可以随时获取该用户的用户名。
Startup.cs ConfigureServices
方法对我来说是最不清楚的地方,而且很可能是某些地方设置不正确的地方。
Startup.cs ConfigureServices():
public void ConfigureServices(IServiceCollection services)
{
// Add identity types
services.AddIdentity<User, Role>()
//.AddUserManager<UserManager<User>>() // some other settings I've tried ...
//.AddRoleManager<RoleManager<Role>>()
//.AddUserStore<UserStore>()
//.AddRoleStore<RoleStore>()
//.AddRoles<Role>()
.AddDefaultTokenProviders();
// Identity Services
services.AddTransient<IUserStore<User>, UserStore>();
services.AddTransient<IRoleStore<Role>, RoleStore>();
//for SQL connection, I'll be using a different one (not the one from the link to topic)
//dependency injection
services.AddScoped<ISomeService, SomeService>();
services.AddAuthentication(sharedOptions =>
{
// authentication options...
})
.AddWsFederation(options =>
{
// wsfed options...
})
.AddCookie(options =>
{
options.Cookie.Name = "NameOfCookie";
//options.LoginPath = "/Access/Login"; //app function the same without this
options.LogoutPath = "/Access/Logout";
options.AccessDeniedPath = "/Access/AccessDenied";
options.ExpireTimeSpan = TimeSpan.FromMinutes(120);
options.SlidingExpiration = true;
});
services.AddControllersWithViews();
}
另一种方法是将custom attribute store添加到ADFS。
然后您可以将自定义属性存储中所需的角色等配置为声明。