private async Task <string> GenerateToken(LoginResponseDto loginResponse) { var userName = loginResponse.UserName; var addExpiryMinutes = int.Parse(_config["SetExpiryTimeInterval:ExpiryInMinutes"]); var claims = GetClaims(loginResponse, userName, addExpiryMinutes); var appSecretKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_config["Audience:Secret"])); var token = new TokenBuilder() .AddSecurityKey(appSecretKey) .AddSubject(loginResponse.UserName) .AddSubject(userName) .AddIssuer(_config["Audience:Iss"]) .AddAudience(_config["Audience:Aud"]) .AddExpiry(addExpiryMinutes) // expiry is in minutes aligned as in InternetServices project for 14 Days 60 * 24 * 14 .AddClaims(claims) .Build(); // Below lines added to add user token information for further validation var userTokenData = new AddUserTokenControllerRequest { UserName = loginResponse.UserName, TokenValue = token.Value, CreatedBy = loginResponse.UserName }; if (!string.IsNullOrEmpty(userTokenData.UserName)) { userTokenData.UserName = userTokenData.UserName.LowerCase(); } if (string.IsNullOrEmpty(userTokenData.TokenValue)) { throw new BadRequestException("Token not generated"); } var userTokenInfo = new IM_TOKENS_CONTROLLER { USER_NAME = userTokenData.UserName, VALUE = userTokenData.TokenValue, ISTOKENVALID = true, CreatedBy = userTokenData.CreatedBy, CreatedDate = DateTime.Now, ID = Guid.NewGuid().ToString() }; _transactionalUnitOfWork.SetIsActive(true); _tokenControllerRepository.Add(userTokenInfo); await _transactionalUnitOfWork.CommitAsync(); return(token.Value); }
public async Task <ServiceResponse> UserRegistration(UserRegistrationRequestDto request) { var response = new ServiceResponse { ErrorList = new List <ErrorMessage>() }; request.FirstName = request.FirstName?.TitleCase(); request.LastName = request.LastName?.TitleCase(); request.UserName = request.UserName?.LowerCase(); request.Password = _encryptData.EncryptPassword(request.Password); request.UserEmail = request.UserEmail?.LowerCase(); request.Remarks = request.Remarks?.TitleCase(); request.Active = true; var user = _mapper.Map <IM_USERS>(request); // to fix an issue related to claims generation during login if (string.IsNullOrEmpty(user.LASTNAME)) { user.LASTNAME = ""; } user.CreatedBy = "SYSTEM"; user.CreatedDate = DateTime.Now; user.ID = Guid.NewGuid().ToString(); _transactionalUnitOfWork.SetIsActive(false); await _transactionalUnitOfWork.CommitAsync(); _userRepository.Add(user); var contributorGroup = await _groupsRepository.GetReadOnlyAsync(x => x.NAME == "CONTRIBUTORS"); if (contributorGroup == null) { throw new BadRequestException("Unable to assign access to user"); } var addUserGroup = new IM_USERS_GROUPS { ID = Guid.NewGuid().ToString(), CreatedBy = "ADMIN", CreatedDate = DateTime.Now, GROUP_ID = contributorGroup.ID, ACTIVE = true, USER_NAME = user.USERNAME }; _transactionalUnitOfWork.SetIsActive(true); _usersGroupsRepository.Add(addUserGroup); var committedRows = await _transactionalUnitOfWork.CommitAsync(); if (committedRows > 0) { response.Success = true; response.Msg = "User registered successfully"; } else { response.Success = false; response.Msg = "Failed to register user"; } var recipients = new List <EmailRecipientDto> { new EmailRecipientDto { RecipientName = $"{user.FIRSTNAME} {user.LASTNAME}", RecipientEmailAddress = user.USEREMAIL } }; var newUserRegistrationEmailTemplate = await _templateSettingRepository.GetReadOnlyAsync(x => x.KEY == "NEW_USER_TEMPLATE"); if (newUserRegistrationEmailTemplate != null) { var emailTemplateString = newUserRegistrationEmailTemplate.VALUE; if (!string.IsNullOrEmpty(emailTemplateString)) { var emailHtmlBody = emailTemplateString.Replace("{username}", $"{user.FIRSTNAME} {user.LASTNAME}") // TODO: get application ui url from appsettings .Replace("{link}", "https://www.google.com"); await EmailNotificationHelper.SendEmailNotification(_configuration.GetSection("SendGridAPIKey").Value, newUserRegistrationEmailTemplate.NAME, emailHtmlBody, "", recipients); } } return(await Task.Run(() => response)); }