/// <summary> /// Convert from View to Claim Identity /// </summary> /// <param name="ClaimIdentity">Claim Identity View</param> /// <returns>Instance of System.Security.Claims.ClaimsIdentity</returns> public static ClaimsIdentity ConvertToClaimsIdentity(this ClaimIdentityView ClaimIdentity) { string authenticationType = EnumStringValue.GetStringValue(ClaimIdentity.AuthenticationType); ClaimsIdentity identity = new ClaimsIdentity(authenticationType, UserNameClaimType, RoleClaimType); foreach (ClaimView item in ClaimIdentity.ClaimViewList) { identity.AddClaim(new Claim(item.Type, item.Value, item.ValueType)); } return(identity); }
/// <summary> /// Create a user without or without a password and return a ClaimsIdentity representing the user created. /// </summary> /// <param name="request">Instance of CreateRequest</param> /// <returns>Instance of CreateResponse</returns> public async Task <CreateResponse> CreateAsync(CreateRequest request) { CreateResponse response = new CreateResponse(); try { AspNetUser newUser = new AspNetUser() { Id = Guid.NewGuid().ToString(), FirstName = request.FirstName, LastName = request.LastName, UserName = request.UserName, Discriminator = "ApplicationUser" //Change as per your requirements }; IdentityResult result; if (!string.IsNullOrWhiteSpace(request.Password)) { result = await this.UserManager.CreateAsync(newUser, request.Password); } else { result = await this.UserManager.CreateAsync(newUser); } if (result.Succeeded) { string authType = EnumStringValue.GetStringValue(request.AuthenticationType); ClaimsIdentity identity = await this.UserManager.CreateIdentityAsync(newUser, authType); response.UserId = new Guid(newUser.Id); response.ClaimIdentity = identity.ConvertToClaimIdentityView(); response.Success = true; } else { response.AddErrors(response.Errors); response.Success = false; } } catch (Exception e) { response.Success = false; response.Errors.Add(e.Message); } return(response); }
/// <summary> /// Login User by external provider and return ClaimsIdentity /// </summary> /// <param name="request">Instance of LoginExternalRequest</param> /// <returns>Instance of LoginExternalResponse</returns> public async Task <LoginExternalResponse> LoginExternalAsync(LoginExternalRequest request) { LoginExternalResponse response = new LoginExternalResponse(); try { string authType = EnumStringValue.GetStringValue(request.AuthenticationType); AspNetUser user = await UserManager.FindAsync(new UserLoginInfo(request.LoginProvider, request.ProviderKey)); ClaimsIdentity identity = await UserManager.CreateIdentityAsync(user, authType); response.ClaimIdentity = identity.ConvertToClaimIdentityView(); response.Success = true; } catch (Exception e) { response.Success = false; response.Errors.Add(e.Message); } return(response); }
/// <summary> /// Create a ClaimsIdentity representing an existing user. /// </summary> /// <param name="request">Instance of CreateRequest</param> /// <returns>Instance of CreateResponse</returns> public async Task <CreateIdentityResponse> CreateIdentityAsync(CreateIdentityRequest request) { CreateIdentityResponse response = new CreateIdentityResponse(); try { string authType = EnumStringValue.GetStringValue(request.AuthenticationType); AspNetUser user = await this.UserManager.FindByNameAsync(request.UserName); ClaimsIdentity identity = await this.UserManager.CreateIdentityAsync(user, authType); response.ClaimIdentityView = identity.ConvertToClaimIdentityView(); response.Success = true; } catch (Exception e) { response.Success = false; response.Errors.Add(e.Message); } return(response); }