/// <summary> /// Gets a value indicating whether the specified user is in the specified role for the configured applicationName. /// </summary> /// <param name="username">The user name to search for.</param> /// <param name="roleName">The role to search in.</param> /// <returns>true if the specified user is in the specified role for the configured applicationName; otherwise, false.</returns> public override bool IsUserInRole(string username, string roleName) { try { using (InventoryManagementEntities context = new InventoryManagementEntities()) { if (!EFMembershipProvider.CheckUser(username, ApplicationName, context)) { return(false); } return((from u in context.aspnet_Membership where u.aspnet_Users.UserName == username && u.aspnet_Applications.ApplicationName == ApplicationName from r in u.aspnet_Users.aspnet_Roles where r.RoleName == roleName && r.aspnet_Applications.ApplicationName == ApplicationName select r).Count() > 0); } } catch (Exception ex) { if (WriteExceptionsToEventLog) { WriteToEventLog(ex, "IsUserInRole"); } throw; } }
/// <summary> /// Gets a list of the roles that a specified user is in for the configured applicationName. /// </summary> /// <param name="username">The user to return a list of roles for.</param> /// <returns>A string array containing the names of all the roles that the specified user is in for the configured applicationName.</returns> public override string[] GetRolesForUser(string username) { using (InventoryManagementEntities context = new InventoryManagementEntities()) { if (!EFMembershipProvider.CheckUser(username, ApplicationName, context)) { throw new ArgumentNullException("username"); } return((from u in context.aspnet_Membership where u.aspnet_Users.UserName == username && u.aspnet_Applications.ApplicationName == ApplicationName from r in u.aspnet_Users.aspnet_Roles where r.aspnet_Applications.ApplicationName == ApplicationName select r.RoleName).ToArray()); } }
void Initialize(EFMembershipProvider <TUser, TKey> Provider);
public IHttpActionResult ValidateUser(string username, string password) { try { UserAccountStatus result = UserAccountStatus.NotSet; EFMembershipProvider membershipprovider = new EFMembershipProvider(); membershipprovider.Initialize("SqlProvider", new NameValueCollection()); MembershipUser user = membershipprovider.GetUser(username, false); DateTime lastLoginDate = DateTime.MinValue; if (user != null) { //Get the last login date lastLoginDate = user.LastLoginDate; } if (membershipprovider.ValidateUser(username, password)) { using (var ctx = new InventoryManagementEntities()) { //check if the employer, user is with, is active: if (ctx.Users != null && ctx.Users.Any() && ctx.Users.Any(c => c.Email == user.UserName && c.ActiveFL == true) == false) { result = UserAccountStatus.InactiveEmployer; return(Ok(result)); } //Update user table with last login date if (user != null) { if (user.ProviderUserKey != null) { Guid.TryParse(user.ProviderUserKey.ToString(), out Guid userid); User loginUser = ctx.Users.FirstOrDefault(c => c.ID == userid); if (loginUser != null) { loginUser.LastLoginDate = lastLoginDate; ctx.SaveChanges(); } } } } result = UserAccountStatus.Success; return(Ok(result)); } if (user != null) { //user is locked out due to too many incorrect login attempts: if (user.IsLockedOut) { return(Ok(UserAccountStatus.LockedOut)); } //user account is inactive: return(Ok(!user.IsApproved ? UserAccountStatus.Inactive : UserAccountStatus.InvalidCredentials)); } result = UserAccountStatus.AccountNotFound; return(Ok(result)); } catch (Exception e) { throw new Exception("Error validating user", e); } }
public void Initialize( #if USE_WEBMATRIX EFMembershipProvider <TUser, TOAuthMembership, TKey> Provider