【问题标题】:Using windows identity to check users custom role using the .IsInRole method使用 Windows 身份使用 .IsInRole 方法检查用户自定义角色
【发布时间】:2013-11-28 14:10:28
【问题描述】:

我创建了一个 MVC 应用程序,用户可以通过 ADFS 或 Forms 登录进行身份验证,并且在这两种情况下,我都能够使用 User.IsInRole 方法检查我的用户表,该表具有关联的 roleID 属性我的数据库中的角色表。这是通过在我的 webconfig 中包含以下部分来完成的:

<roleManager enabled="true" defaultProvider="DefaultRoleProvider" cacheRolesInCookie="true">
  <providers>
    <clear />
    <add name="DefaultRoleProvider" type="MyInternal.Providers.MyProvider, MyInternal" connectionStringName="MyContext" />
  </providers>
</roleManager>

我现在正在尝试实施 Windows 身份验证并已设法获取用户域登录名等,但是当尝试按照与其他两种身份验证类型相同的步骤时,我无法让 IsInRole 工作。

如何将存储库中的用户绑定到身份验证用户。是否有某种类型的演员或需要的东西?我认为这可能是通过 ADFS 和 Forms 中的身份验证完成的。

【问题讨论】:

    标签: security asp.net-mvc-4 windows-authentication roleprovider principal


    【解决方案1】:

    我能够通过在我的 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;
                }
    

    【讨论】:

      猜你喜欢
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 2014-08-08
      • 2013-12-06
      • 2012-04-10
      • 1970-01-01
      • 2016-08-24
      相关资源
      最近更新 更多