Security 使用windows标识使用.IsInRole方法检查用户自定义角色
我创建了一个MVC应用程序,用户可以通过ADFS或Forms login进行身份验证,在这两种情况下,我都可以使用user.IsInRole方法检查我的用户表,该用户表具有roleID属性,该属性与数据库中的角色表相关联。这是通过在我的网络配置中包含以下部分来实现的:Security 使用windows标识使用.IsInRole方法检查用户自定义角色,security,asp.net-mvc-4,windows-authentication,roleprovider,principal,Security,Asp.net Mvc 4,Windows Authentication,Roleprovider,Principal,我创建了一个MVC应用程序,用户可以通过ADFS或Forms login进行身份验证,在这两种情况下,我都可以使用user.IsInRole方法检查我的用户表,该用户表具有roleID属性,该属性与数据库中的角色表相关联。这是通过在我的网络配置中包含以下部分来实现的: <roleManager enabled="true" defaultProvider="DefaultRoleProvider" cacheRolesInCookie="true"> <providers&
<roleManager enabled="true" defaultProvider="DefaultRoleProvider" cacheRolesInCookie="true">
<providers>
<clear />
<add name="DefaultRoleProvider" type="MyInternal.Providers.MyProvider, MyInternal" connectionStringName="MyContext" />
</providers>
</roleManager>
我现在正在尝试实现windows身份验证,并已成功获取用户的域登录名等,但在尝试执行与其他两种身份验证类型相同的步骤时,我无法使IsInRole正常工作
如何将存储库中的用户绑定到身份验证用户。是否需要一些类型的石膏或其他东西?我认为这可能是通过ADF和表单中的身份验证完成的。我能够通过在ViewModel中使用以下内容来解决此问题:
this.UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
if (this.UserName.Contains("\\"))
{
string[] stringArray = this.UserName.Split(new Char[] { '\\' });
this.UserName = stringArray[1];
MyUser identity = userRepository.Get(u => u.Username == this.UserName).FirstOrDefault();
HttpContext.Current.User = identity;
}
我可以通过在ViewModel中使用以下工具来解决此问题:
this.UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
if (this.UserName.Contains("\\"))
{
string[] stringArray = this.UserName.Split(new Char[] { '\\' });
this.UserName = stringArray[1];
MyUser identity = userRepository.Get(u => u.Username == this.UserName).FirstOrDefault();
HttpContext.Current.User = identity;
}