Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Roles.GetRolesForUser在自定义授权文件中不可用_C#_Asp.net_Asp.net Mvc_Authorization_Identity - Fatal编程技术网

C# Roles.GetRolesForUser在自定义授权文件中不可用

C# Roles.GetRolesForUser在自定义授权文件中不可用,c#,asp.net,asp.net-mvc,authorization,identity,C#,Asp.net,Asp.net Mvc,Authorization,Identity,我正在尝试实现自己的自定义属性,其中我必须获取当前用户的所有角色,如下所示: public class CustomRoleAuthorization: System.Web.Mvc.AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { base.OnAuthorization(filterContext

我正在尝试实现自己的自定义属性,其中我必须获取当前用户的所有角色,如下所示:

public class CustomRoleAuthorization: System.Web.Mvc.AuthorizeAttribute
    {

   public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                filterContext.Result = new RedirectResult("~/Login");
                return;
            }
            var requiredRoles = Roles.Split(Convert.ToChar(",")).ToList();

            var userRoles = Roles.GetRolesForUser(filterContext.HttpContext.User.Identity.Name);

            foreach (var item in requiredRoles)
            {
                if (!filterContext.HttpContext.User.IsInRole(item))
                {
                    filterContext.Result = new RedirectResult("~/Index/Index");
                    return;
                }
            }

        }
}
但由于某些原因,这一行行不通:

 var userRoles = Roles.GetRolesForUser(filterContext.HttpContext.User.Identity.Name);
它说Roles属性是一个字符串,它不包含GetRolesForUser方法

如何将此扩展方法添加到我的项目中,以便在登录时从identity获取所有用户角色

@Stephen这是我在登录时设置角色的方式:

 if (user.PasswordHash == PasswordSecurity.CreatePasswordHash(model.Password, user.PasswordSalt))
 {
  ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
 identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, model.Email));
  List<Claim> claims = new List<Claim>();
 var roles = user.UserRoles.Where(x=>x.Active==true).ToList();
 foreach (var item in roles)
 {
   claims.Add(new Claim(ClaimTypes.Role, item.Roles.RoleName));
  }
 identity.AddClaims(claims);
 identity.AddClaim(new Claim(ClaimTypes.Name, model.Email));
 AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddHours(3) }, identity);
 return Json("ok");
 }
if(user.PasswordHash==PasswordSecurity.CreatePasswordHash(model.Password,user.PasswordSalt))
{
ClaimsIdentity identity=新的ClaimsIdentity(DefaultAuthenticationTypes.applicationcokie);
identity.AddClaim(新声明(ClaimTypes.NameIdentifier,model.Email));
列表声明=新列表();
var roles=user.UserRoles.Where(x=>x.Active==true.ToList();
foreach(角色中的变量项)
{
添加(新索赔(ClaimTypes.Role,item.Roles.RoleName));
}
身份。添加索赔(索赔);
identity.AddClaim(新索赔(ClaimTypes.Name,model.Email));
AuthenticationManager.SignIn(新的AuthenticationProperties{IsPersistent=true,ExpiresUtc=DateTimeOffset.UtcNow.AddHours(3)},标识);
返回Json(“ok”);
}

AuthorizeAttribute
包含一个
公共字符串角色{get;set;}
属性(请参阅),因此需要使用完全限定名

var userRoles = System.Web.Security.Roles.GetRolesForUser(...

或者您可以为程序集名称创建别名

嘿,Stephen,非常感谢您的回复!顺便说一下,我做了你刚才写的,结果得到:{string[0]}?看起来我没有正确设置角色或者?这表明您没有当前登录用户的角色(但不确定是否有其他原因导致它返回空数组)。我已编辑了我的问题,其中包括如何添加用户角色Your using
ClaimSidenty
,我认为有不同的代码用于获取所有角色(给我10分钟时间检查)检查答案