public async Task <ApiResponse <UserOld> > FindUser(UserOld user) { var response = new ApiResponse <UserOld>(); var passwordManger = new PasswordManager(); try { string password = passwordManger.Encrypt(user.Password); var userRes = await _usersRepository.FindAsync(i => i.UserName == user.UserName && i.Password == password); if (userRes == null) { response.Success = true; response.Errors.Add("invalid User"); return(response); } response.Success = true; response.Data = userRes; return(response); } catch (Exception ex) { response.Success = false; response.Errors.Add(ex.Message); return(response); } }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var allowedOrigin = context.OwinContext.Get <string>("as:clientAllowedOrigin") ?? "*"; context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin }); var userService = new UserOldService(); var userDto = new UserOld { UserName = context.UserName, Password = context.Password }; var user = await userService.FindUser(userDto); if (user.Data == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } string roles = string.Join(",", user.Data.Roles.Select(x => x.Role).ToArray()); var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName)); identity.AddClaim(new Claim(ClaimTypes.Role, roles)); var props = new AuthenticationProperties(new Dictionary <string, string> { { "client_id", context.ClientId ?? string.Empty }, { "UserEmail", context.UserName }, { "UserId", user.Data.Id.ToString() }, { "Roles", roles } }); var ticket = new AuthenticationTicket(identity, props); context.Validated(ticket); }