使用MembershipProvider
重新审视用户创建中的一些旧代码,我发现没有使用以下代码创建新用户:
// the call
Membership.CreateUser(user.Username, user.Password, user.EmailAddress, null, null, false, null, out createStatus);
public override MembershipUser CreateUser(string username,
string password, string email, string passwordQuestion,
string passwordAnswer, bool isApproved,
object providerUserKey, out MembershipCreateStatus status)
{
using (var db = new DbV001Context())
{
var crypto = new SimpleCrypto.PBKDF2();
var encryptedPw = crypto.Compute(password);
var user = db.Users.Create();
user.MembershipTypeId = 7;
user.UserName = username;
user.Email = email;
user.Password = encryptedPw;
user.PasswordSalt = crypto.Salt;
//user.CreateDate = DateTime.Now; // set automatically in the db
//user.CreateDate = new DateTime(1980,01,01,0,0,0,0);
user.LastLoginDate = new DateTime(1980,01,01,0,0,0,0);
user.LastActivityDate = new DateTime(1980,01,01,0,0,0,0);
user.LastPasswordChangedDate = new DateTime(1980,01,01,0,0,0,0);
user.LastLockoutDate = new DateTime(1980, 01, 01, 0, 0, 0, 0);
db.Users.Add(user);
try
{
db.SaveChanges();
} ...
我在实现repository and UoW pattern之前编写了这部分代码,我在其他地方使用它:
private readonly IUnitOfWork _unitOfWork;
private readonly IRepository<User> _repository;
public AccountController() { }
public AccountController(IUnitOfWork unitOfWorkAsync,
IRepository<User> repository)
{
_unitOfWork = unitOfWorkAsync;
_repository = repository;
}
.
.
.
var userEntity = _repository
.ODataQueryable()
.Where(u => (u.Email == user.EmailAddress | u.UserName == user.Username)
&& u.Email != null).ToList();
我尝试了以下操作,但在尝试Insert()
时仍然遇到异常:
public class CustomMembershipProvider : MembershipProvider
{
//private DbV001Context _db = new DbV001Context();
private readonly IUnitOfWork _unitOfWork;
private readonly IRepository<User> _repository;
public CustomMembershipProvider() { }
public CustomMembershipProvider(IUnitOfWork unitOfWorkAsync,
IRepository<User> repository)
{
_unitOfWork = unitOfWorkAsync;
_repository = repository;
}
public override MembershipUser CreateUser(string username,
string password, string email, string passwordQuestion,
string passwordAnswer, bool isApproved,
object providerUserKey, out MembershipCreateStatus status)
{
//using (var db = new DbContext())
//{
var crypto = new SimpleCrypto.PBKDF2();
var encryptedPw = crypto.Compute(password);
//var user = db.Users.Create();
var user = new User();
user.MembershipTypeId = 7;
user.UserName = username;
user.Email = email;
user.Password = encryptedPw;
user.PasswordSalt = crypto.Salt;
//user.CreateDate = DateTime.Now; // set automatically in the db
//user.CreateDate = new DateTime(1980,01,01,0,0,0,0);
user.LastLoginDate = new DateTime(1980,01,01,0,0,0,0);
user.LastActivityDate = new DateTime(1980,01,01,0,0,0,0);
user.LastPasswordChangedDate = new DateTime(1980,01,01,0,0,0,0);
user.LastLockoutDate = new DateTime(1980, 01, 01, 0, 0, 0, 0);
//db.Users.Add(user);
try
{
//db.SaveChanges();
_repository.InsertGraph(user); //Object reference not set to an instance of an object.
}
因为我看了这些东西已经有一段时间了,我如何更新上面的代码来使用存储库,而不是数据库上下文?另外,既然我正在使用DI,那么在设置CustomMembershipProvider
时我是否缺少某些内容?
当我单步执行代码时,CustomMembershipProvider
构造函数未被执行。使用Autofac进行DI。
答案 0 :(得分:0)
我不认为成员资格提供程序允许您在构造函数中提供参数...在您的参数less构造函数中,您可以为您的UOW和Repository分配当前实例,或者如果尚未完成则激活它们。< / p>
但这取决于你正在使用的DI框架。