public async Task MarkUserAsLoggedInAsync( IUserCredentialsInput credentials, string accessToken) { var jwtSecurityToken = new JwtSecurityTokenHandler().ReadJwtToken(accessToken); var roleName = jwtSecurityToken.Claims .FirstOrDefault(c => c.Type.Equals(ClaimTypes.Role)) ?.Value; await LocalStorage.SetItemAsync(Identity.LocalStorageKeys.User.UserName, credentials.UserName); await LocalStorage.SetItemAsync(Identity.LocalStorageKeys.User.RoleName, roleName); await LocalStorage.SetItemAsync(Identity.LocalStorageKeys.Auth.AccessToken, accessToken); var patienReferenceIdAsString = jwtSecurityToken.Claims .FirstOrDefault(c => c.Type.Equals("PatientReferenceId")) ?.Value; var patienReferenceId = patienReferenceIdAsString != null ? new Guid(patienReferenceIdAsString) : Guid.Empty; await LocalStorage.SetItemAsync(Scheduling.LocalStorageKeys.Patient.ReferenceId, patienReferenceId); var claimsPrincipal = await GetClaimsPrincipalAsync(); NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(claimsPrincipal))); }
public async Task <string> GenerateJwtAsync(IUserCredentialsInput userInfo, string roleName) { var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Config["Jwt:SecretKey"])); var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); int expiryInMinutes = Convert.ToInt32(Config["Jwt:ExpiryInMinutes"]); var claims = new List <Claim> { new Claim(JwtRegisteredClaimNames.Sub, userInfo.UserName), new Claim(JwtRegisteredClaimNames.UniqueName, userInfo.UserName.ToString()), new Claim(ClaimTypes.Role, roleName), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), }; if (roleName.Equals(RoleType.Patient.ToString())) { var patient = await PatientRepository.SingleOrDefaultAsync(p => p.IdentityUser.UserName == userInfo.UserName); var patientClaims = new Claim("PatientReferenceId", patient.ReferenceId.ToString()); claims.Add(patientClaims); } else if (roleName.Equals(RoleType.Dentist.ToString())) { var dentalTeamReferenceId = await DentalWorkerRepository.AsNoTracking() .Where(dw => dw.IdentityUser.UserName == userInfo.UserName) .Join( DentalTeamParticipantRepository.AsNoTracking(), (dw) => dw.Id, (dtp) => dtp.ParticipantId, (dw, dtp) => new { dtp.TeamId } ).Join( DentalTeamRepository.AsNoTracking(), (q) => q.TeamId, (dt) => dt.Id, (q, dt) => new { dt.ReferenceId } ).SingleOrDefaultAsync(); var dentalWorkerClaims = new Claim("DentalTeamReferenceId", dentalTeamReferenceId.ReferenceId.ToString()); claims.Add(dentalWorkerClaims); } var token = new JwtSecurityToken( issuer: Config["Jwt:Issuer"], audience: Config["Jwt:Audience"], claims: claims, expires: DateTime.UtcNow.AddMinutes(expiryInMinutes), signingCredentials: credentials ); return(new JwtSecurityTokenHandler().WriteToken(token)); }
public async Task <IResult <IAccessTokenOutput> > LoginAsync(IUserCredentialsInput input) { var response = await HttpClient.PostAsJsonAsync <IUserCredentialsInput>("api/Auth/login", input); var responseStatusCode = response.StatusCode; if (responseStatusCode == HttpStatusCode.BadRequest) { var errors = await response.Content.ReadFromJsonAsync <IEnumerable <ValidationError> >(); return(new Result <IAccessTokenOutput>(errors)); } var accessToken = await response.Content.ReadFromJsonAsync <AccessTokenOutput>(); return(new Result <IAccessTokenOutput>(accessToken)); }
public async Task <IResult <IAccessTokenOutput> > LoginAsync(IUserCredentialsInput userInput) { var validationResult = Validator.Validate(userInput); if (validationResult.Errors.Count > 0) { return(new Result <IAccessTokenOutput>(validationResult.Errors)); } var user = await UserService.FindByNameAsync(userInput.UserName); if (user == null) { validationResult.Errors.Add( new ValidationError() { PropertyName = nameof(IUserCredentialsInput.UserName), Errors = new [] { "User does not exist." } } ); } if (user != null && !(await UserService.CheckPasswordAsync(user, userInput.Password))) { validationResult.Errors.Add( new ValidationError() { PropertyName = nameof(IUserCredentialsInput.Password), Errors = new [] { "Invalid password." } } ); } if (validationResult.Errors.Count > 0) { return(new Result <IAccessTokenOutput>(validationResult.Errors)); } var roleName = (await UserService.GetRolesAsync(user)).FirstOrDefault(); var tokenString = await JwtAuthManager.GenerateJwtAsync(userInput, roleName); return(new Result <IAccessTokenOutput>(new AccessTokenOutput(tokenString))); }
public Task <IResult <IAccessTokenOutput> > RegisterUserAsync(IUserCredentialsInput input) { throw new System.NotImplementedException(); }