加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

在ASP.NET MVC 5中向Windows角色添加自定义角色

发布时间:2020-12-16 03:49:35 所属栏目:asp.Net 来源:网络整理
导读:我正在使用ASP.NET MVC 5构建一个Intranet应用程序. 我的目标是对Active Directory所做的任何用户进行身份验证(即我使用“Windows身份验证”),然后将组添加到应用程序内的任何用户(不使用域组). 我在这里找到了一些非常有趣的代码: http://brockallen.com/2
我正在使用ASP.NET MVC 5构建一个Intranet应用程序.

我的目标是对Active Directory所做的任何用户进行身份验证(即我使用“Windows身份验证”),然后将组添加到应用程序内的任何用户(不使用域组).

我在这里找到了一些非常有趣的代码:

http://brockallen.com/2013/01/17/adding-custom-roles-to-windows-roles-in-asp-net-using-claims/

但它在我的场景中不起作用:当我使用[Authorize(Role =“AppRole”)]装饰控制器时,即使用户(使用声明)与“AppRole”角色相关联,我也无法获得授权.

这是我的代码:

在Global.asax.cs中

void Application_PostAuthenticateRequest()
    {
        if (Request.IsAuthenticated)
        {

            string[] roles = Utils.GetRolesForUser(User.Identity.Name);

            var id = ClaimsPrincipal.Current.Identities.First();
            foreach (var role in roles)
            {
                //id.AddClaim(new Claim(ClaimTypes.Role,role.ToString()));
                id.AddClaim(new Claim(ClaimTypes.Role,@"Kairos.milCompliance"));
            }


            bool pippo = User.IsInRole("Compliance");

            HttpContext.Current.User = (IPrincipal)id ;

            bool pippo2 = User.IsInRole("Compliance");


        }
    }

函数GetRolesForUser如下(并且工作正常):

public static string[] GetRolesForUser(string username)
    {
        dbOrdiniPersonaliEntities db = new dbOrdiniPersonaliEntities();

        string utente = StripDomain(username);

        string[] gruppi = new string[db.vGruppiUtentis.Where(t => t.KairosLogin == utente).Count()];

        int i=0;

        foreach (var gruppo in db.vGruppiUtentis.Where(t => t.KairosLogin == utente))
        {

            gruppi[i]=gruppo.GruppoDes;
            i=i++;
        }
        return gruppi;
    }

控制器使用“标准”Authorize子句进行修饰:

[Authorize(Roles="AppRole")]
    public ActionResult Index(string sortOrder,string currentFilter,string DesSearchString,int? page)
    {
      // my code here
    }

任何的想法?

提前致谢

UPDATE

谢谢@Leandro
我试过你提出以下代码

void Application_PostAuthenticateRequest()
    {
        if (Request.IsAuthenticated)
        {

            string[] roles = Utils.GetRolesForUser(User.Identity.Name);

            ClaimsIdentity id = ClaimsPrincipal.Current.Identities.First();
            foreach (var role in roles)
            {
                //id.AddClaim(new Claim(ClaimTypes.Role,@"Kairos.milCompliance"));
            }

            bool pippo = User.IsInRole("Compliance");

            SetPrincipal((IPrincipal)id);

            bool pippo2 = User.IsInRole("Compliance");


        }
    }

但是当代码到达这一点时,我收到运行时错误

SetPrincipal((IPrincipal)id);

错误如下

无法将类型为“System.Security.Principal.WindowsIdentity”的对象强制转换为“System.Security.Principal.IPrincipal”.

谢谢你的帮助

更新2(可能已解决)

你好
深入了解SO,我找到了这个资源

ASP.NET MVC and Windows Authentication with custom roles

根据@Xhalent的回答,我按如下方式修改了我的代码

protected void Application_PostAuthenticateRequest()
    {
        if (Request.IsAuthenticated)
        {
            String[] roles = Utils.GetRolesForUser(User.Identity.Name);

            GenericPrincipal principal = new GenericPrincipal(User.Identity,roles);

            Thread.CurrentPrincipal = HttpContext.Current.User = principal;
        }
    }

看来现在工作正常!任何意见?有什么缺点吗?非常感谢!!

解决方法

使用此方法保存主体,因此它也在线程中设置:

private void SetPrincipal(IPrincipal principal)
        {
            Thread.CurrentPrincipal = principal;
            if (HttpContext.Current != null)
            {
                HttpContext.Current.User = principal;
            }
        }

更新:还允许匿名并测试User.IsInRole是否在方法内部获得了某些内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读