/// <summary> /// Registers user (typically with default claims) /// </summary> /// <param name="userRegistration">User registration details</param> /// <param name="createAdmin">Grant user admin rights</param> /// <returns>ID of registered user</returns> public Guid RegisterUserAccount(UserRegistrationDTO userRegistration, bool createAdmin = false) { using (UnitOfWorkProvider.Create()) { var userClaims = new List <Claim>(); if (createAdmin) { userClaims.Add(new Claim(ClaimTypes.Role, Claims.Admin)); } else { // for the moment there is just Player role left userClaims.Add(new Claim(ClaimTypes.Role, Claims.Player)); } var account = coreService.CreateAccount(userRegistration.UserName, userRegistration.Password, userRegistration.Email, null, null); AutoMapper.Mapper.Map(userRegistration, account); foreach (var claim in userClaims) { coreService.AddClaim(account.ID, claim.Type, claim.Value); } coreService.Update(account); return(account.ID); } }
public Func <TAccount, string, UserManagementResult> SetForClaim(string type) { return((account, value) => { try { _userAccountService.RemoveClaim(account.ID, type); if (!String.IsNullOrWhiteSpace(value)) { _userAccountService.AddClaim(account.ID, type, value); } } catch (ValidationException ex) { return new UserManagementResult(new[] { ex.Message }); } return UserManagementResult.Success; }); }
public void SetRolesForUser(string userName, IEnumerable <string> roles) { var user = userSvc.GetByUsername(userName); if (user != null) { userSvc.RemoveClaim(user.ID, ClaimTypes.Role); if (roles != null) { foreach (var role in roles) { userSvc.AddClaim(user.ID, ClaimTypes.Role, role); } } } }
public ActionResult Index(string gender) { if (String.IsNullOrWhiteSpace(gender)) { userAccountService.RemoveClaim(User.GetUserID(), ClaimTypes.Gender); } else { // if you only want one of these claim types, uncomment the next line //account.RemoveClaim(ClaimTypes.Gender); userAccountService.AddClaim(User.GetUserID(), ClaimTypes.Gender, gender); } // since we've changed the claims, we need to re-issue the cookie that // contains the claims. authSvc.SignIn(User.GetUserID()); return(RedirectToAction("Index")); }