protected override void LoadTestData() { var username1 = $"*****@*****.**"; var username2 = $"*****@*****.**"; _user1 = new ApplicationUser { UserName = username1, Email = username1, EmailConfirmed = true, NormalizedUserName = username1.ToUpperInvariant() }; _user2 = new ApplicationUser { UserName = username2, Email = username2, EmailConfirmed = true, NormalizedUserName = username2.ToUpperInvariant() }; Context.Users.Add(_user1); Context.Users.Add(_user2); var claimForUser1 = new IdentityUserClaim<string> { UserId = _user1.Id, ClaimType = ClaimTypes.ProfileIncomplete, ClaimValue = "NewUser" }; Context.UserClaims.Add(claimForUser1); var claimForUser2 = new IdentityUserClaim<string> { UserId = _user2.Id, ClaimType = "SomeOtherClaim", ClaimValue = "Blah" }; Context.UserClaims.Add(claimForUser2); Context.SaveChanges(); }
public void CanCreateUserClaimBasedOnSecurityClaim() { var claim = new IdentityUserClaim(BasicClaim); claim.ClaimType.ShouldBe(BasicClaim.Type); claim.ClaimValue.ShouldBe(BasicClaim.Value); }
public IdentityUserClaimWrapper(IdentityUserClaim userClaim) { this.Id = userClaim.Id; this.UserId = userClaim.UserId; this.ClaimValue = userClaim.ClaimValue; this.ClaimType = userClaim.ClaimType; }
public void CanConvertToSecurityClaim() { var claim = new IdentityUserClaim(BasicClaim); var securityClaim = claim.ToClaim(); securityClaim.Type.ShouldBe(claim.ClaimType); securityClaim.Value.ShouldBe(claim.ClaimValue); }
public IdentityUserClaim Convert() { var userClaim = new IdentityUserClaim(); userClaim.Id = this.Id; userClaim.UserId = this.UserId; userClaim.ClaimValue = this.ClaimValue; userClaim.ClaimType = this.ClaimType; return userClaim; }
/// <summary> /// Adds or Updates an existing claim /// </summary> /// <param name="ClaimType">ClaimType</param> /// <param name="ClaimValue">ClaimValue</param> public static void AddOrUpdateClaim(this ICollection<IdentityUserClaim> userClaims, string ClaimType, string ClaimValue) { var currentClaim = userClaims.FirstOrDefault(x => x.ClaimType == ClaimType); if (currentClaim == null) { currentClaim = new IdentityUserClaim() { ClaimType = ClaimType }; userClaims.Add(currentClaim); } currentClaim.ClaimValue = ClaimValue; }
public async Task<IdentityResult> CreateAsync(User u) { var user = new User { Email = u.Email, Password = u.Password, Id = u.Id, HolidayTimeStart = DateTime.Now, HolidayTimeEnd = DateTime.Now + new TimeSpan(10, 0, 0, 0), DayOfRegistration = DateTime.Now, BusinessTrip = false, EmailConfirmed = u.EmailConfirmed, UserName = u.Email }; var result = await _applicationUserManager.CreateAsync(user, u.Password); if (!result.Succeeded) return result; var identityClaim = new IdentityUserClaim {ClaimType = ClaimTypes.Role, ClaimValue = "user"}; user.Claims.Add(identityClaim); result = await _applicationUserManager.UpdateAsync(user); return result; }
public async Task<ActionResult> Edit(AccountInfoVm Model) { if (ModelState.IsValid) { var userId = User.Identity.GetUserId(); var user = await UserManager.FindByIdAsync(userId); user.UserName = Model.Username; user.Email = Model.Email; user.PhoneNumber = Model.PhoneNumber; if (Model.FullName != null) { IdentityUserClaim FullNameClaim = user.Claims.Where(x => x.ClaimType == "FullName").FirstOrDefault(); if (FullNameClaim != null) { FullNameClaim.ClaimValue = Model.FullName; } else { FullNameClaim = new IdentityUserClaim() { ClaimType = "FullName", ClaimValue = Model.FullName }; } user.Claims.Add(FullNameClaim); } if (Model.Sex != null) { IdentityUserClaim GenderClaim = user.Claims.Where(x => x.ClaimType == ClaimTypes.Gender).FirstOrDefault(); if (GenderClaim != null) { GenderClaim.ClaimValue = Model.Sex; } else { GenderClaim = new IdentityUserClaim() { ClaimType = ClaimTypes.Gender, ClaimValue = Model.Sex }; } user.Claims.Add(GenderClaim); } if (Model.BirthDate.HasValue) { IdentityUserClaim BirthDateClaim = user.Claims.Where(x => x.ClaimType == ClaimTypes.DateOfBirth).FirstOrDefault(); if (BirthDateClaim != null) { BirthDateClaim.ClaimValue = Model.BirthDate.Value.ToString(@"dd\/MM\/yyyy"); } else { BirthDateClaim = new IdentityUserClaim() { ClaimType = ClaimTypes.DateOfBirth, ClaimValue = Model.BirthDate.Value.ToString(@"dd\/MM\/yyyy") }; } user.Claims.Add(BirthDateClaim); } await UserManager.UpdateAsync(user); return RedirectToAction("Edit",new { Message = SystemMessage.SaveAccountInfoSuccess}); } else { ViewBag.StatusMessage = new ViewSystemMessage(SystemMessage.Error); return View("Edit", Model); } }
public void CanCreateUserClaim() { var claim = new IdentityUserClaim(); claim.ShouldNotBe(null); }
/// <summary> /// Registers the user. /// </summary> /// <param name="userModel">The user model.</param> /// <param name="actorClaim">The actor claim.</param> /// <returns></returns> private async Task<IdentityResult> RegisterUser(UserModel userModel, string actorClaim) { try { var user = new IdentityUser { UserName = userModel.UserName }; var claim = new IdentityUserClaim {ClaimType = ClaimTypes.Actor, ClaimValue = actorClaim}; claim.UserId = user.Id; user.Claims.Add(claim); var result = await _userManager.CreateAsync(user, userModel.Password); return result; } catch (DbEntityValidationException) { return IdentityResult.Failed(); } }
private static void ProcessUser(ref TempUser temp) { lock (ObjectLock) { Console.WriteLine(migrateOption ? "Processing User: {0}" : "Analyzing User: {0}", temp.User.Id); Console.WriteLine("Roles: {0}, Claims: {1}, Logins: {2}", temp.Roles.Count, temp.Claims.Count, temp.Logins.Count); if (temp.Roles.Count > 0) { var newRoles = temp.Roles.Select(r => { var item = new IdentityUserRole(); JsonConvert.PopulateObject(r.ToString(), item); return item; }).ToList(); var eRoles = temp.User.GetPropertyValue<List<IdentityUserRole>>("Roles"); if (eRoles != null) { eRoles.AddRange(newRoles.Where(n => !eRoles.Any(er => er.Id == n.Id))); temp.User.SetPropertyValue("Roles", eRoles.ToArray()); } else { temp.User.SetPropertyValue("Roles", newRoles.ToArray()); } } if (temp.Claims.Count > 0) { var newClaims = temp.Claims.Select(r => { var item = new IdentityUserClaim(); JsonConvert.PopulateObject(r.ToString(), item); return item; }).ToList(); var eClaims = temp.User.GetPropertyValue<List<IdentityUserClaim>>("Claims"); if (eClaims != null) { eClaims.AddRange(newClaims.Where(n => !eClaims.Any(er => er.Id == n.Id))); temp.User.SetPropertyValue("Claims", eClaims.ToArray()); } else { temp.User.SetPropertyValue("Claims", newClaims.ToArray()); } } if (temp.Logins.Count > 0) { var newLogins = temp.Logins.Select(r => { var item = new IdentityUserLogin(); JsonConvert.PopulateObject(r.ToString(), item); return item; }).ToList(); var eLogins = temp.User.GetPropertyValue<List<IdentityUserLogin>>("Logins"); if (eLogins != null) { eLogins.AddRange(newLogins.Where(n => !eLogins.Any(er => er.Id == n.Id))); temp.User.SetPropertyValue("Logins", eLogins.ToArray()); } else { temp.User.SetPropertyValue("Logins", newLogins.ToArray()); } } } }
public void IdentityUserClaimGet_UserId() { var uc = new IdentityUserClaim(); uc.GenerateKeys(); Assert.AreEqual(uc.PartitionKey, uc.UserId, "PartitionKey and UserId are not equal."); }