ASP.NET&Spring.NET&NHibernate最佳实践(二十)——第4章权限子系统(13)
成员资格信息提供类(HibernateMembershipProvider.cs)
/// <summary>
/// 向成员资格数据库添加一个新用户
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="email"></param>
/// <param name="passwordQuestion"></param>
/// <param name="passwordAnswer"></param>
/// <param name="isApproved"></param>
/// <param name="providerUserKey"></param>
/// <param name="status"></param>
/// <returns></returns>
public override MembershipUser CreateUser(string username, string password, string email,
string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey,
out MembershipCreateStatus status)
{
if (!SecUtility.ValidateParameter(ref password, true, true, false, 128))
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
string salt = GenerateSalt();
string pass = EncodePassword(password, this._passwordFormat, salt);
if (pass.Length > 128)
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
if (passwordAnswer != null)
{
passwordAnswer = passwordAnswer.Trim();
}
string answer;
if (!String.IsNullOrEmpty(passwordAnswer))
{
if (passwordAnswer.Length > 128)
{
status = MembershipCreateStatus.InvalidAnswer;
return null;
}
answer = EncodePassword(passwordAnswer.ToLower(CultureInfo.InvariantCulture), this._passwordFormat, salt);
}
else
{
answer = passwordAnswer;
}
if (!SecUtility.ValidateParameter(ref answer, this._requiresQuestionAndAnswer, true, false, 128))
{
status = MembershipCreateStatus.InvalidAnswer;
return null;
}
if (!SecUtility.ValidateParameter(ref username, true, true, true, 255))
{
status = MembershipCreateStatus.InvalidUserName;
return null;
}
if (!SecUtility.ValidateParameter(ref email, this._requiresUniqueEmail, this._requiresUniqueEmail, false, 255))
{
status = MembershipCreateStatus.InvalidEmail;
return null;
}
if (!SecUtility.ValidateParameter(ref passwordQuestion, this._requiresQuestionAndAnswer, true, false, 255))
{
status = MembershipCreateStatus.InvalidQuestion;
return null;
}
if ((providerUserKey != null) && !(providerUserKey is Int32))
{
status = MembershipCreateStatus.InvalidProviderUserKey;
return null;
}
if (password.Length < this._minRequiredPasswordLength)
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
int nonAlphanumericCharacters = 0;
for (int i = 0; i < password.Length; i++)
{
if (!char.IsLetterOrDigit(password, i))
{
nonAlphanumericCharacters++;
}
}
if (nonAlphanumericCharacters < this._minRequiredNonAlphanumericCharacters)
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
if ((this._passwordStrengthRegularExpression.Length > 0) && !Regex.IsMatch(password, this._passwordStrengthRegularExpression))
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, password, true);
this.OnValidatingPassword(args);
if (args.Cancel)
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
DateTime currentTimeUtc = this.RoundToSeconds(DateTime.UtcNow);
int userID;
status = (MembershipCreateStatus)CreateUser(this._applicationName, username, pass, salt, email,
passwordQuestion, answer, isApproved, this._requiresUniqueEmail, this._passwordFormat, currentTimeUtc, out userID);
if (status != 0)
{
return null;
}
currentTimeUtc = currentTimeUtc.ToLocalTime();
return new MembershipUser(this.Name, username, userID, email, passwordQuestion, null, isApproved, false, currentTimeUtc, currentTimeUtc, currentTimeUtc, currentTimeUtc, new DateTime(1754, 1, 1));
}
/// <summary>
/// 从成员资格数据库删除用户的成员资格信息
/// </summary>
/// <param name="username"></param>
/// <param name="deleteAllRelatedData"></param>
/// <returns></returns>
public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
SecUtility.CheckParameter(ref username, true, true, true, 255, "username");
int status = ServiceLocator.UserService.DeleteUser(this._applicationName, username, deleteAllRelatedData);
if (status != 0)
{
return false;
}
return true;
}
/// <summary>
/// 返回成员资格用户的集合,其中用户的电子邮件地址字段包含指定的电子邮件地址
/// </summary>
/// <param name="emailToMatch"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="totalRecords"></param>
/// <returns></returns>
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex,
int pageSize, out int totalRecords)
{
SecUtility.CheckParameter(ref emailToMatch, false, false, false, 128, "emailToMatch");
MembershipUserCollection membershipUsers = new MembershipUserCollection();
if (pageIndex < 0)
{
throw new ArgumentException("PageIndex bad.");
}
if (pageSize < 1)
{
throw new ArgumentException("PageSize bad.");
}
long lBound = (long)pageIndex * pageSize;
long uBound = lBound + pageSize - 1;
if (uBound > Int32.MaxValue)
{
throw new ArgumentException("PageIndex,pageSize bad.");
}
IList users = ServiceLocator.UserService.FindUsersByEmail(this._applicationName, emailToMatch, pageIndex,
pageSize, out totalRecords);
foreach (User user in users)
{
MembershipUser membershipUser = new MembershipUser(this.Name, user.Username, user.ID, user.Email,
user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreateDate.ToLocalTime(),
user.LastLoginDate.ToLocalTime(), user.LastActivityDate.ToLocalTime(),
user.LastPasswordChangedDate.ToLocalTime(), user.LastLockoutDate.ToLocalTime());
membershipUsers.Add(membershipUser);
}
return membershipUsers;
}
/// <summary>
/// 获取一个成员资格用户的集合,这些用户的用户名包含要匹配的指定用户名
/// </summary>
/// <param name="usernameToMatch"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="totalRecords"></param>
/// <returns></returns>
public override MembershipUserCollection FindUsersByName(string usernameToMatch,
int pageIndex, int pageSize, out int totalRecords)
{
SecUtility.CheckParameter(ref usernameToMatch, true, true, false, 255, "usernameToMatch");
MembershipUserCollection membershipUsers = new MembershipUserCollection();
if (pageIndex < 0)
{
throw new ArgumentException("PageIndex bad.");
}
if (pageSize < 1)
{
throw new ArgumentException("PageSize bad.");
}
long lBound = (long)pageIndex * pageSize;
long uBound = lBound + pageSize - 1;
if (uBound > Int32.MaxValue)
{
throw new ArgumentException("PageIndex,pageSize bad.");
}
IList users = ServiceLocator.UserService.FindUsersByName(this._applicationName, usernameToMatch, pageIndex,
pageSize, out totalRecords);
foreach (User user in users)
{
MembershipUser membershipUser = new MembershipUser(this.Name, user.Username, user.ID, user.Email,
user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreateDate.ToLocalTime(),
user.LastLoginDate.ToLocalTime(), user.LastActivityDate.ToLocalTime(),
user.LastPasswordChangedDate.ToLocalTime(), user.LastLockoutDate.ToLocalTime());
membershipUsers.Add(membershipUser);
}
return membershipUsers;
}
/// <summary>
/// 获取成员资格数据库中所有用户的集合
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="totalRecords"></param>
/// <returns></returns>
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize,
out int totalRecords)
{
MembershipUserCollection membershipUsers = new MembershipUserCollection();
if (pageIndex < 0)
{
throw new ArgumentException("PageIndex bad.");
}
if (pageSize < 1)
{
throw new ArgumentException("PageSize bad.");
}
long lBound = (long)pageIndex * pageSize;
long uBound = lBound + pageSize - 1;
if (uBound > Int32.MaxValue)
{
throw new ArgumentException("PageIndex,pageSize bad.");
}
long lTotalRecords;
IList users = ServiceLocator.UserService.GetAllUsers(this._applicationName, pageIndex,
pageSize, out lTotalRecords);
totalRecords = Convert.ToInt32(lTotalRecords);
foreach (User user in users)
{
MembershipUser membershipUser = new MembershipUser(this.Name, user.Username, user.ID, user.Email,
user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreateDate.ToLocalTime(),
user.LastLoginDate.ToLocalTime(), user.LastActivityDate.ToLocalTime(),
user.LastPasswordChangedDate.ToLocalTime(), user.LastLockoutDate.ToLocalTime());
membershipUsers.Add(membershipUser);
}
return membershipUsers;
}
/// <summary>
/// 返回当前访问该应用程序的用户数
/// </summary>
/// <returns></returns>
public override int GetNumberOfUsersOnline()
{
return ServiceLocator.UserService.GetNumberOfUsersOnline(this._applicationName,
Membership.UserIsOnlineTimeWindow, DateTime.UtcNow);
}
/// <summary>
/// 从成员资格数据库返回指定用户名的密码
/// </summary>
/// <param name="username"></param>
/// <param name="answer"></param>
/// <returns></returns>
public override string GetPassword(string username, string passwordAnswer)
{
if (!this._enablePasswordRetrieval)
{
throw new NotSupportedException("Membership password retrieval not supported.");
}
SecUtility.CheckParameter(ref username, true, true, true, 255, "username");
string answer = GetEncodedPasswordAnswer(username, passwordAnswer);
SecUtility.CheckParameter(ref answer, this._requiresQuestionAndAnswer, this._requiresQuestionAndAnswer, false, 128, "passwordAnswer");
MembershipPasswordFormat passwordFormat = MembershipPasswordFormat.Clear;
int status = 0;
string pass = ServiceLocator.UserService.GetPassword(this._applicationName, username, answer, this._requiresQuestionAndAnswer,
this._maxInvalidPasswordAttempts, this._passwordAttemptWindow, out passwordFormat, out status, DateTime.UtcNow);
if (pass == null)
{
string errText = this.GetExceptionText(status);
if (this.IsStatusDueToBadPassword(status))
{
throw new MembershipPasswordException(errText);
}
throw new ProviderException(errText);
}
return UnEncodePassword(pass, passwordFormat);
}
/// <summary>
/// 从数据源获取成员资格用户的信息
/// </summary>
/// <param name="username"></param>
/// <param name="userIsOnline"></param>
/// <returns></returns>
public override MembershipUser GetUser(string username, bool userIsOnline)
{
SecUtility.CheckParameter(ref username, true, false, true, 255, "username");
User user = ServiceLocator.UserService.GetUserByName(this._applicationName, username, userIsOnline, DateTime.UtcNow);
if (user != null)
{
return new MembershipUser(this.Name, user.Username, user.ID, user.Email,
user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreateDate.ToLocalTime(),
user.LastLoginDate.ToLocalTime(), user.LastActivityDate.ToLocalTime(),
user.LastPasswordChangedDate.ToLocalTime(), user.LastLockoutDate.ToLocalTime());
}
else
{
return null;
}
}
/// <summary>
/// 从数据源获取成员资格用户的信息
/// </summary>
/// <param name="providerUserKey"></param>
/// <param name="userIsOnline"></param>
/// <returns></returns>
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
if (providerUserKey == null)
{
throw new ArgumentNullException("providerUserKey.");
}
if (!(providerUserKey is Int32))
{
throw new ArgumentException("Membership invalid provider userKey.");
}
User user = ServiceLocator.UserService.GetUserByUserID((int)providerUserKey, userIsOnline, DateTime.UtcNow);
if (user != null)
{
return new MembershipUser(this.Name, user.Username, user.ID, user.Email,
user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreateDate.ToLocalTime(),
user.LastLoginDate.ToLocalTime(), user.LastActivityDate.ToLocalTime(),
user.LastPasswordChangedDate.ToLocalTime(), user.LastLockoutDate.ToLocalTime());
}
else
{
return null;
}
}
/// <summary>
/// 获取与指定的电子邮件地址关联的用户名
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
public override string GetUserNameByEmail(string email)
{
SecUtility.CheckParameter(ref email, false, false, false, 128, "email");
IList users = ServiceLocator.UserService.GetUsersByEmail(this._applicationName, email);
if (this._requiresUniqueEmail && users.Count > 0)
{
throw new ProviderException("Membership more than one_user with email.");
}
if (users.Count == 0)
{
return null;
}
return ((User)users[0]).Username;
}
/// 向成员资格数据库添加一个新用户
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="email"></param>
/// <param name="passwordQuestion"></param>
/// <param name="passwordAnswer"></param>
/// <param name="isApproved"></param>
/// <param name="providerUserKey"></param>
/// <param name="status"></param>
/// <returns></returns>
public override MembershipUser CreateUser(string username, string password, string email,
string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey,
out MembershipCreateStatus status)
{
if (!SecUtility.ValidateParameter(ref password, true, true, false, 128))
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
string salt = GenerateSalt();
string pass = EncodePassword(password, this._passwordFormat, salt);
if (pass.Length > 128)
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
if (passwordAnswer != null)
{
passwordAnswer = passwordAnswer.Trim();
}
string answer;
if (!String.IsNullOrEmpty(passwordAnswer))
{
if (passwordAnswer.Length > 128)
{
status = MembershipCreateStatus.InvalidAnswer;
return null;
}
answer = EncodePassword(passwordAnswer.ToLower(CultureInfo.InvariantCulture), this._passwordFormat, salt);
}
else
{
answer = passwordAnswer;
}
if (!SecUtility.ValidateParameter(ref answer, this._requiresQuestionAndAnswer, true, false, 128))
{
status = MembershipCreateStatus.InvalidAnswer;
return null;
}
if (!SecUtility.ValidateParameter(ref username, true, true, true, 255))
{
status = MembershipCreateStatus.InvalidUserName;
return null;
}
if (!SecUtility.ValidateParameter(ref email, this._requiresUniqueEmail, this._requiresUniqueEmail, false, 255))
{
status = MembershipCreateStatus.InvalidEmail;
return null;
}
if (!SecUtility.ValidateParameter(ref passwordQuestion, this._requiresQuestionAndAnswer, true, false, 255))
{
status = MembershipCreateStatus.InvalidQuestion;
return null;
}
if ((providerUserKey != null) && !(providerUserKey is Int32))
{
status = MembershipCreateStatus.InvalidProviderUserKey;
return null;
}
if (password.Length < this._minRequiredPasswordLength)
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
int nonAlphanumericCharacters = 0;
for (int i = 0; i < password.Length; i++)
{
if (!char.IsLetterOrDigit(password, i))
{
nonAlphanumericCharacters++;
}
}
if (nonAlphanumericCharacters < this._minRequiredNonAlphanumericCharacters)
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
if ((this._passwordStrengthRegularExpression.Length > 0) && !Regex.IsMatch(password, this._passwordStrengthRegularExpression))
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, password, true);
this.OnValidatingPassword(args);
if (args.Cancel)
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
DateTime currentTimeUtc = this.RoundToSeconds(DateTime.UtcNow);
int userID;
status = (MembershipCreateStatus)CreateUser(this._applicationName, username, pass, salt, email,
passwordQuestion, answer, isApproved, this._requiresUniqueEmail, this._passwordFormat, currentTimeUtc, out userID);
if (status != 0)
{
return null;
}
currentTimeUtc = currentTimeUtc.ToLocalTime();
return new MembershipUser(this.Name, username, userID, email, passwordQuestion, null, isApproved, false, currentTimeUtc, currentTimeUtc, currentTimeUtc, currentTimeUtc, new DateTime(1754, 1, 1));
}
/// <summary>
/// 从成员资格数据库删除用户的成员资格信息
/// </summary>
/// <param name="username"></param>
/// <param name="deleteAllRelatedData"></param>
/// <returns></returns>
public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
SecUtility.CheckParameter(ref username, true, true, true, 255, "username");
int status = ServiceLocator.UserService.DeleteUser(this._applicationName, username, deleteAllRelatedData);
if (status != 0)
{
return false;
}
return true;
}
/// <summary>
/// 返回成员资格用户的集合,其中用户的电子邮件地址字段包含指定的电子邮件地址
/// </summary>
/// <param name="emailToMatch"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="totalRecords"></param>
/// <returns></returns>
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex,
int pageSize, out int totalRecords)
{
SecUtility.CheckParameter(ref emailToMatch, false, false, false, 128, "emailToMatch");
MembershipUserCollection membershipUsers = new MembershipUserCollection();
if (pageIndex < 0)
{
throw new ArgumentException("PageIndex bad.");
}
if (pageSize < 1)
{
throw new ArgumentException("PageSize bad.");
}
long lBound = (long)pageIndex * pageSize;
long uBound = lBound + pageSize - 1;
if (uBound > Int32.MaxValue)
{
throw new ArgumentException("PageIndex,pageSize bad.");
}
IList users = ServiceLocator.UserService.FindUsersByEmail(this._applicationName, emailToMatch, pageIndex,
pageSize, out totalRecords);
foreach (User user in users)
{
MembershipUser membershipUser = new MembershipUser(this.Name, user.Username, user.ID, user.Email,
user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreateDate.ToLocalTime(),
user.LastLoginDate.ToLocalTime(), user.LastActivityDate.ToLocalTime(),
user.LastPasswordChangedDate.ToLocalTime(), user.LastLockoutDate.ToLocalTime());
membershipUsers.Add(membershipUser);
}
return membershipUsers;
}
/// <summary>
/// 获取一个成员资格用户的集合,这些用户的用户名包含要匹配的指定用户名
/// </summary>
/// <param name="usernameToMatch"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="totalRecords"></param>
/// <returns></returns>
public override MembershipUserCollection FindUsersByName(string usernameToMatch,
int pageIndex, int pageSize, out int totalRecords)
{
SecUtility.CheckParameter(ref usernameToMatch, true, true, false, 255, "usernameToMatch");
MembershipUserCollection membershipUsers = new MembershipUserCollection();
if (pageIndex < 0)
{
throw new ArgumentException("PageIndex bad.");
}
if (pageSize < 1)
{
throw new ArgumentException("PageSize bad.");
}
long lBound = (long)pageIndex * pageSize;
long uBound = lBound + pageSize - 1;
if (uBound > Int32.MaxValue)
{
throw new ArgumentException("PageIndex,pageSize bad.");
}
IList users = ServiceLocator.UserService.FindUsersByName(this._applicationName, usernameToMatch, pageIndex,
pageSize, out totalRecords);
foreach (User user in users)
{
MembershipUser membershipUser = new MembershipUser(this.Name, user.Username, user.ID, user.Email,
user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreateDate.ToLocalTime(),
user.LastLoginDate.ToLocalTime(), user.LastActivityDate.ToLocalTime(),
user.LastPasswordChangedDate.ToLocalTime(), user.LastLockoutDate.ToLocalTime());
membershipUsers.Add(membershipUser);
}
return membershipUsers;
}
/// <summary>
/// 获取成员资格数据库中所有用户的集合
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="totalRecords"></param>
/// <returns></returns>
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize,
out int totalRecords)
{
MembershipUserCollection membershipUsers = new MembershipUserCollection();
if (pageIndex < 0)
{
throw new ArgumentException("PageIndex bad.");
}
if (pageSize < 1)
{
throw new ArgumentException("PageSize bad.");
}
long lBound = (long)pageIndex * pageSize;
long uBound = lBound + pageSize - 1;
if (uBound > Int32.MaxValue)
{
throw new ArgumentException("PageIndex,pageSize bad.");
}
long lTotalRecords;
IList users = ServiceLocator.UserService.GetAllUsers(this._applicationName, pageIndex,
pageSize, out lTotalRecords);
totalRecords = Convert.ToInt32(lTotalRecords);
foreach (User user in users)
{
MembershipUser membershipUser = new MembershipUser(this.Name, user.Username, user.ID, user.Email,
user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreateDate.ToLocalTime(),
user.LastLoginDate.ToLocalTime(), user.LastActivityDate.ToLocalTime(),
user.LastPasswordChangedDate.ToLocalTime(), user.LastLockoutDate.ToLocalTime());
membershipUsers.Add(membershipUser);
}
return membershipUsers;
}
/// <summary>
/// 返回当前访问该应用程序的用户数
/// </summary>
/// <returns></returns>
public override int GetNumberOfUsersOnline()
{
return ServiceLocator.UserService.GetNumberOfUsersOnline(this._applicationName,
Membership.UserIsOnlineTimeWindow, DateTime.UtcNow);
}
/// <summary>
/// 从成员资格数据库返回指定用户名的密码
/// </summary>
/// <param name="username"></param>
/// <param name="answer"></param>
/// <returns></returns>
public override string GetPassword(string username, string passwordAnswer)
{
if (!this._enablePasswordRetrieval)
{
throw new NotSupportedException("Membership password retrieval not supported.");
}
SecUtility.CheckParameter(ref username, true, true, true, 255, "username");
string answer = GetEncodedPasswordAnswer(username, passwordAnswer);
SecUtility.CheckParameter(ref answer, this._requiresQuestionAndAnswer, this._requiresQuestionAndAnswer, false, 128, "passwordAnswer");
MembershipPasswordFormat passwordFormat = MembershipPasswordFormat.Clear;
int status = 0;
string pass = ServiceLocator.UserService.GetPassword(this._applicationName, username, answer, this._requiresQuestionAndAnswer,
this._maxInvalidPasswordAttempts, this._passwordAttemptWindow, out passwordFormat, out status, DateTime.UtcNow);
if (pass == null)
{
string errText = this.GetExceptionText(status);
if (this.IsStatusDueToBadPassword(status))
{
throw new MembershipPasswordException(errText);
}
throw new ProviderException(errText);
}
return UnEncodePassword(pass, passwordFormat);
}
/// <summary>
/// 从数据源获取成员资格用户的信息
/// </summary>
/// <param name="username"></param>
/// <param name="userIsOnline"></param>
/// <returns></returns>
public override MembershipUser GetUser(string username, bool userIsOnline)
{
SecUtility.CheckParameter(ref username, true, false, true, 255, "username");
User user = ServiceLocator.UserService.GetUserByName(this._applicationName, username, userIsOnline, DateTime.UtcNow);
if (user != null)
{
return new MembershipUser(this.Name, user.Username, user.ID, user.Email,
user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreateDate.ToLocalTime(),
user.LastLoginDate.ToLocalTime(), user.LastActivityDate.ToLocalTime(),
user.LastPasswordChangedDate.ToLocalTime(), user.LastLockoutDate.ToLocalTime());
}
else
{
return null;
}
}
/// <summary>
/// 从数据源获取成员资格用户的信息
/// </summary>
/// <param name="providerUserKey"></param>
/// <param name="userIsOnline"></param>
/// <returns></returns>
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
if (providerUserKey == null)
{
throw new ArgumentNullException("providerUserKey.");
}
if (!(providerUserKey is Int32))
{
throw new ArgumentException("Membership invalid provider userKey.");
}
User user = ServiceLocator.UserService.GetUserByUserID((int)providerUserKey, userIsOnline, DateTime.UtcNow);
if (user != null)
{
return new MembershipUser(this.Name, user.Username, user.ID, user.Email,
user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreateDate.ToLocalTime(),
user.LastLoginDate.ToLocalTime(), user.LastActivityDate.ToLocalTime(),
user.LastPasswordChangedDate.ToLocalTime(), user.LastLockoutDate.ToLocalTime());
}
else
{
return null;
}
}
/// <summary>
/// 获取与指定的电子邮件地址关联的用户名
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
public override string GetUserNameByEmail(string email)
{
SecUtility.CheckParameter(ref email, false, false, false, 128, "email");
IList users = ServiceLocator.UserService.GetUsersByEmail(this._applicationName, email);
if (this._requiresUniqueEmail && users.Count > 0)
{
throw new ProviderException("Membership more than one_user with email.");
}
if (users.Count == 0)
{
return null;
}
return ((User)users[0]).Username;
}