/// <summary> /// This is to check user identity /// </summary> /// <param name="email"></param> /// <param name="password"></param> /// <returns></returns> /// <exception cref="ArgumentException"></exception> public TokenResponse GetIdentity(string email, string password) { Account account = AccountRepository.Get(x => x.Email == email).SingleOrDefault(); if (account == null) { throw new ArgumentNullException($"Email not found {email}"); } bool isValidPassword = passwordSalter.EqualsSequence(password, account.Hash); if (!isValidPassword) { throw new ArgumentException("Invalid password"); } string nameId = account.AccountId.ToString(); email = account.Email; string defaultRole = account.Role.ToString(); IEnumerable <Claim> claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, nameId), new Claim(ClaimsIdentity.DefaultRoleClaimType, defaultRole) }; var claimsIdentity = new ClaimsIdentity(claims); var response = new TokenResponse(claimsIdentity); return(response); }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <exception cref="ArgumentNullException">Account not found</exception> /// <exception cref="ArgumentException">Password not match</exception> /// <returns></returns> public override Task <TokenReply> HandleRequest(RequestObject <TokenRequest, TokenReply> request, CancellationToken cancellationToken = default) { return(Task.Run(() => { TokenRequest tokenRequest = request.Value; // check username and password var requestedAccount = Context .Accounts .Where(a => a.Email.Equals(tokenRequest.Email, StringComparison.InvariantCultureIgnoreCase)) .Select(c => new { c.AccountId, c.Role, c.Hash }) .SingleOrDefault(); if (requestedAccount == null || !passwordSalter.EqualsSequence(tokenRequest.Password, requestedAccount.Hash)) { request.StatusCode = StatusCode.NotFound; request.Detail = $"{nameof(tokenRequest.Password)} is not match"; return new TokenReply(); } TokenResponse tokenResponce = new TokenResponse(requestedAccount.AccountId, requestedAccount.Role, jwtProvider); TokenReply tokenReply = new TokenReply() { UserId = tokenResponce.UserId, Token = tokenResponce.Token }; return tokenReply; })); }
/// <summary> /// This is to check user identity /// </summary> /// <param name="email"></param> /// <param name="password"></param> /// <returns></returns> /// <exception cref="ArgumentException"></exception> public Task <TokenResponse> GetIdentity(string email, string password) { // find account by email Account account = accountRepository .GetWithInclude( x => x.Email.Equals(email) && passwordSalter.EqualsSequence(password, x.Hash), u => u.User) .SingleOrDefault(); return(GetIdentity(account)); }