public async Task <Client> FindClientByIdAsync(string clientId) { hPay_Demo_HSAEntities db = new hPay_Demo_HSAEntities(); var clientFromDb = db.IdentityClients.SingleOrDefault(x => x.Client_Id == clientId && x.Enabled == true); Client client = new Client(); if (clientFromDb != null) { var clientSecretFromDb = db.IdentityClientSecrets.FirstOrDefault(x => x.Client_Id == clientFromDb.Id && x.Expiration > System.DateTime.Now); if (clientSecretFromDb != null) { client.ClientId = clientFromDb.Client_Id; client.ClientName = clientFromDb.Client_Name; client.Enabled = clientFromDb.Enabled; client.AccessTokenType = AccessTokenType.Reference; client.Flow = Flows.ClientCredentials; client.ClientSecrets = new List <Secret> { new Secret(clientSecretFromDb.Client_Secret.Sha256()) }; client.AllowedScopes = new List <string> { clientFromDb.Client_Scope }; client.AccessTokenLifetime = clientFromDb.AccessTokenLifetime; } } return(client); }
public async Task <IEnumerable <Scope> > GetScopesAsync(bool publicOnly = true) { hPay_Demo_HSAEntities data = new hPay_Demo_HSAEntities(); var scopes = from s in data.IdentityServerScopes.Include(x => x.Enabled == true) select s; List <Scope> foundScopes = new List <Scope>(); foreach (var scope in scopes) { var name = scope; var dbScopeTask = data.IdentityServerScopes.FirstOrDefaultAsync(e => e.Name == name.Name); await dbScopeTask; var dbScope = dbScopeTask.Result; if (dbScope == null) { continue; } Scope scopeToAdd = new Scope(); scopeToAdd.Name = name.Name; foundScopes.Add(scopeToAdd); } foundScopes.AddRange(StandardScopes.All); return(foundScopes); // Very similar to above, but converting all db scopes to idsvr3 scopes }
public bool ResetPassword(string userLogin) { bool isSuccessful = false; User user = GetUser(userLogin); if (user != null && (user.Deactivated == null || user.Deactivated == false)) { try { string resetPasswordToken = GetNewPasswordToken(); user.PasswordResetExpiration = DateTime.Now.AddDays(1); user.PasswordResetToken = resetPasswordToken; using (var db = new hPay_Demo_HSAEntities()) { db.Entry(user).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } SendPasswordViaEmail(resetPasswordToken); isSuccessful = true; } catch (Exception ex) { isSuccessful = false; } } return(isSuccessful); }
public async Task <IEnumerable <Scope> > FindScopesAsync(IEnumerable <string> scopeNames) { hPay_Demo_HSAEntities data = new hPay_Demo_HSAEntities(); List <Scope> foundScopes = new List <Scope>(); foreach (var scope in scopeNames) { var name = scope; var dbScopeTask = data.IdentityServerScopes.FirstOrDefaultAsync(e => e.Name == name && e.Enabled == true); await dbScopeTask; var dbScope = dbScopeTask.Result; if (dbScope == null) { continue; } Scope scopeToAdd = new Scope(); scopeToAdd.Name = name; scopeToAdd.DisplayName = dbScope.DisplayName; // logic to create idsvr3 Scope model from db model foundScopes.Add(scopeToAdd); } foundScopes.AddRange(StandardScopes.All); return(foundScopes); }
public User AuthenticateUser(string userName, string password) { using (var hPayEntities = new hPay_Demo_HSAEntities()) { var hPayUser = hPayEntities.Users.FirstOrDefault(u => u.UserLogin == userName && u.Pswd == password && u.IsApproved == true); return(hPayUser); } }
public Client GetClient(string clientId) { using (var hPayEntities = new hPay_Demo_HSAEntities()) { var hPayClient = hPayEntities.Clients.FirstOrDefault(u => u.Id == clientId); return(hPayClient); } }
public User GetUser(string userName) { using (var hPayEntities = new hPay_Demo_HSAEntities()) { var hPayUser = hPayEntities.Users.FirstOrDefault(u => u.UserLogin == userName); hPayEntities.Entry <User>(hPayUser).Reload(); return(hPayUser); } }
public void DeleteRefreshToken(RefreshToken token) { using (var hPayEntities = new hPay_Demo_HSAEntities()) { hPayEntities.RefreshTokens.Attach(token); hPayEntities.RefreshTokens.Remove(token); //hPayEntities.RefreshTokens.Remove(token); hPayEntities.SaveChanges(); } }
public RefreshToken GetRefreshToken(string refreshTokenId) { //DateTime currentDateTimeInUtcFormaTime = TimeZoneInfo.ConvertTimeToUtc(System.DateTime.Now); using (var hPayEntities = new hPay_Demo_HSAEntities()) { var refreshToken = hPayEntities.RefreshTokens.FirstOrDefault( u => u.Id == refreshTokenId); return(refreshToken); } }
public bool DeleteRefreshTokenByRefreshTokenId(string refreshTokenId) { using (var hPayEntities = new hPay_Demo_HSAEntities()) { var refreshToken = hPayEntities.RefreshTokens.FirstOrDefault(u => u.Id == refreshTokenId); var status = false; if (refreshToken != null) { hPayEntities.RefreshTokens.Remove(refreshToken); hPayEntities.SaveChanges(); status = true; } return(status); } }
public bool ChangePassword(string userName, string oldPassword, string newPassword) { using (var hPayEntities = new hPay_Demo_HSAEntities()) { var hPayUser = hPayEntities.Users.FirstOrDefault(u => u.UserLogin == userName && u.Pswd == oldPassword); var status = false; if (hPayUser != null) { hPayUser.Pswd = newPassword; hPayEntities.Users.AddOrUpdate(hPayUser); hPayEntities.SaveChanges(); status = true; } return(status); } }
public bool ForgetPassword(string userName) { string password = string.Empty; bool status = false; using (var hPayEntities = new hPay_Demo_HSAEntities()) { var hPayUser = hPayEntities.Users.FirstOrDefault(u => u.UserLogin == userName); if (hPayUser != null) { ResetPassword(userName); status = true; } return(status); } }
public bool ChangeResetPassword(string userName, string tempPassword, string newPassword) { bool resetChangePasswordStatus = false; using (var hPayEntities = new hPay_Demo_HSAEntities()) { var hPayUser = hPayEntities.Users.FirstOrDefault(u => u.UserLogin == userName && u.PasswordResetToken == tempPassword && u.IsApproved == true); if (hPayUser != null) { hPayUser.Pswd = newPassword; hPayEntities.Entry(hPayUser).State = System.Data.Entity.EntityState.Modified; hPayEntities.SaveChanges(); resetChangePasswordStatus = true; } } return(resetChangePasswordStatus); }
public bool UpdateUserLoginInFlag(string userName, bool isUserLogIn) { using (var hPayEntities = new hPay_Demo_HSAEntities()) { var user = hPayEntities.Users.FirstOrDefault(u => u.UserLogin == userName); var status = false; if (user != null) { user.IsLoggedIn = isUserLogIn; hPayEntities.Entry(user).State = System.Data.Entity.EntityState.Modified; hPayEntities.SaveChanges(); status = true; } return(status); } }
public bool IsUserLoggedIn(string userName) { bool isUserLoggedIn = false; using (var hPayEntities = new hPay_Demo_HSAEntities()) { var hPayUser = hPayEntities.Users.FirstOrDefault(u => u.UserLogin == userName); if (hPayUser.IsLoggedIn != null) { if (hPayUser.IsLoggedIn == true) { isUserLoggedIn = true; } else { isUserLoggedIn = false; } } } return(isUserLoggedIn); }
public bool AddRefreshToken(RefreshToken refreshToken) { bool status = false; try { using (var db = new hPay_Demo_HSAEntities()) { var existingToken = db.RefreshTokens.Where(r => r.Subject == refreshToken.Subject && r.ClientId == refreshToken.ClientId).SingleOrDefault(); if (existingToken != null) { db.RefreshTokens.Attach(existingToken); db.RefreshTokens.Remove(existingToken); db.SaveChanges(); } RefreshToken _refreshToken = new RefreshToken(); _refreshToken.Id = refreshToken.Id; _refreshToken.Subject = refreshToken.Subject; _refreshToken.ClientId = refreshToken.ClientId; _refreshToken.IssuedUtc = refreshToken.IssuedUtc; _refreshToken.ExpiresUtc = refreshToken.ExpiresUtc; _refreshToken.ProtectedTicket = refreshToken.ProtectedTicket; db.RefreshTokens.Add(_refreshToken); db.SaveChanges(); status = true; } } catch (Exception ex) { string exception = ex.Message; status = false; } return(status); }