Exemple #1
0
        public Task <IdentityResult> Create(User user, string password, UserRoleEnum role)
        {
            //add claims & roles
            var ir = _userManager.CreateAsync(user, password);

            ir = _userManager.AddToRoleAsync(user, role.ToString());
            var newClaim = new Claim(role.ToString(), role.ToString(), ClaimValueTypes.String);

            ir = _userManager.AddClaimAsync(user, newClaim);
            return(ir);
        }
 public static IHtmlContent PartialWithRole(this IHtmlHelper helper, string partialName, object model, ClaimsPrincipal user, UserRoleEnum userRole)
 {
     if (user.IsInRole(userRole.ToString()))
     {
         return helper.Partial(partialName, model: model);
     }
     else
     {
         return helper.Raw(string.Empty);
     }
 }
Exemple #3
0
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            var user = filterContext.HttpContext.User as ClaimsPrincipal;

            if (user != null && user.HasClaim("Role", claimValue.ToString()))
            {
                base.OnAuthorization(filterContext);
            }
            else
            {
                base.HandleUnauthorizedRequest(filterContext);
            }
        }
Exemple #4
0
        public static ApplicationUser UpdateUserRole(ApplicationDbContext db, IPrincipal user, UserRoleEnum role)
        {
            string          id = user.Identity.GetUserId();
            ApplicationUser applicationUser = db.Users.FirstOrDefault(x => x.Id == id);

            applicationUser.CurrentUserRole = role;

            db.Entry(applicationUser).State = EntityState.Modified;
            db.SaveChanges();

            //reset the claims to the updated value
            var Identity = user.Identity as ClaimsIdentity;

            Identity.RemoveClaim(Identity.FindFirst("CurrentUserRole"));
            Identity.AddClaim(new Claim("CurrentUserRole", role.ToString()));
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;

            authenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(new ClaimsPrincipal(Identity), new AuthenticationProperties()
            {
                IsPersistent = true
            });

            return(applicationUser);
        }
Exemple #5
0
 public static string GetName(this UserRoleEnum role) => role.ToString();
Exemple #6
0
 private VmRoleInfo GetRoleInfo(UserRoleEnum role)
 {
     return(userRolesCache.GetRole(role.ToString()) ?? new VmRoleInfo());
 }
Exemple #7
0
 public bool IsInRole(UserRoleEnum userRoleEnum)
 {
     return(_httpContextAccessor.HttpContext.User.IsInRole(userRoleEnum.ToString("G")));
 }