gpt4 book ai didi

asp.net - 我*必须*在 MVC3 中覆盖多少 MembershipProvider

转载 作者:行者123 更新时间:2023-12-01 12:57:17 25 4
gpt4 key购买 nike

到目前为止,我已经在我的 MVC3 应用程序中完成了所有身份验证工作,即通过我的 MemberRepository 类验证成员和创建成员。我现在想正式使用自定义 MembershipProvider。到目前为止,我只了解到我确实需要重写此类的 ValidateUser 方法,而且由于我没有使用登录控件,所以我什至不确定我是否一定要这样做。

覆盖方法,如 GetUserCreateUser 为我的聚会带来了不请自来的类型,如 MembershipUser,我有一个精心制作的 Member 类。如果我不打算使用任何内置控件或管理工具,请有人帮我弄清楚我是否真的需要自定义成员(member)提供程序,如果我这样做,我是否应该将覆盖限制在绝对必要的范围内, 这是什么?

最佳答案

这是我为单元测试编写的一个。它尽可能少。

public class MockMembershipProvider : MembershipProvider
{
public IList<MembershipUser> Users { get; private set; }

private string _applicationName;
public override string ApplicationName
{
get
{
return _applicationName;
}
set
{
_applicationName = value;
}
}

public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
throw new NotImplementedException();
}

public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
{
throw new NotImplementedException();
}

public override MembershipUser CreateUser(
string username,
string password,
string email,
string passwordQuestion,
string passwordAnswer,
bool isApproved,
object providerUserKey,
out MembershipCreateStatus status)
{
var user = new MembershipUser(ProviderName, username, username, email, passwordQuestion, null, isApproved, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
Users.Add(user);
status = MembershipCreateStatus.Success;
return user;
}

public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
var u = Users.Where(mu => mu.UserName.Equals(username, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (u == null) return false;
Users.Remove(u);
return true;
}

public override bool EnablePasswordReset
{
get { return false; }
}

public override bool EnablePasswordRetrieval
{
get { return false; }
}

public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}

public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
var users = (from u in Users
where u.UserName.Equals(usernameToMatch, StringComparison.OrdinalIgnoreCase)
select u).ToList();
totalRecords = users.Count;
return ToMembershipUserCollection(users);
}

public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
var list = Users.Skip(pageIndex * pageSize).Take(pageSize);
totalRecords = list.Count();
var result = new MembershipUserCollection();
foreach (var u in list)
{
result.Add(u);
}
return result;
}

public override int GetNumberOfUsersOnline()
{
return Users.Count();
}

public override string GetPassword(string username, string answer)
{
throw new NotImplementedException();
}

public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
return (from u in Users
where u.ProviderUserKey.ToString() == providerUserKey.ToString()
select u).FirstOrDefault();
}

public override MembershipUser GetUser(string username, bool userIsOnline)
{
return (from u in Users
where u.UserName.Equals(username, StringComparison.OrdinalIgnoreCase)
select u).FirstOrDefault();
}

public override string GetUserNameByEmail(string email)
{
return (from u in Users
where u.Email.Equals(email, StringComparison.OrdinalIgnoreCase)
select u.UserName).FirstOrDefault();
}

public override int MaxInvalidPasswordAttempts
{
get { return 3; }
}

public override int MinRequiredNonAlphanumericCharacters
{
get { return 1; }
}

public override int MinRequiredPasswordLength
{
get { return 6; }
}

public override int PasswordAttemptWindow
{
get { return 10; }
}

public override MembershipPasswordFormat PasswordFormat
{
get { throw new NotImplementedException(); }
}

public override string PasswordStrengthRegularExpression
{
get { return null; }
}

public override string Name
{
get
{
return ProviderName;
}
}

public string ProviderName { get; set; }

public override string ResetPassword(string username, string answer)
{
throw new NotImplementedException();
}

public override bool RequiresQuestionAndAnswer
{
get { return false; }
}

public override bool RequiresUniqueEmail
{
get { return true; }
}

private MembershipUserCollection ToMembershipUserCollection(IEnumerable<MembershipUser> users)
{
var result = new MembershipUserCollection();
foreach (var u in users)
{
result.Add(u);
}
return result;
}

public override bool UnlockUser(string userName)
{
return true;
}

public override void UpdateUser(MembershipUser user)
{
var oldUser = Users.Where(u => u.UserName.Equals(user.UserName, StringComparison.OrdinalIgnoreCase)).Single();
var index = Users.IndexOf(oldUser);
Users[index] = user;
}

public override bool ValidateUser(string username, string password)
{
throw new NotImplementedException();
}

public MockMembershipProvider()
{
this.ProviderName = "MockMembershipProvider";
Users = new List<MembershipUser>();
}
}

public class FakeMembershipProvider : MockMembershipProvider
{
public FakeMembershipProvider(string name)
{
this.ProviderName = name ?? "MockMembershipProvider";
}
public override MembershipUser CreateUser(
string username,
string password,
string email,
string passwordQuestion,
string passwordAnswer,
bool isApproved,
object providerUserKey,
out MembershipCreateStatus status)
{
status = MembershipCreateStatus.ProviderError;
var user = new MockMembershipUser();
user.Password = password;
user.User = username;
user.UserKey = providerUserKey;
Users.Add(user);
status = MembershipCreateStatus.Success;
return user;
}
}

public class MockMembershipUser : MembershipUser
{
public string Password { get; set; }
public string User { get; set; }
public object UserKey { get; set; }

public override string UserName { get { return User; } }

public override string Comment { get; set; }

public override object ProviderUserKey { get { return UserKey; } }

public override string GetPassword()
{
return Password ?? string.Empty;
}

关于asp.net - 我*必须*在 MVC3 中覆盖多少 MembershipProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9035804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com