从此解决方案继续:How to configure Ninject for MVC4 & custom Membership provide?
我宣布
public interface IUserRepository : IRepository<UserModel>
{
MembershipUser CreateUser(string username, ... , string providername = null);
void Logout();
Boolean Login(string userName, string Password, bool persistCookie = false);
bool RegisterUser(UserModel user);
}
在 UserRepository
中实施public class UserRepository : RepositoryBase<MyDbContext, UserModel>, IUserRepository
{
UserModel _user = null;
public UserRepository(IUnitOfWork<MyDbContext> unitOfWork)
: base(unitOfWork)
{
}
public MembershipUser CreateUser(string username, ... , string providername = null)
{
using (UnitOfWork)
{
_user = new UserModel
{
Id = Guid.NewGuid(),
RoleId = roleId
[other property set]
};
Insert(_user);
UnitOfWork.Commit();
}
status = MembershipCreateStatus.Success;
return new MembershipUser(providername, ...);
}
public bool RegisterUser(UserModel user)
{
MembershipCreateStatus createStatus;
CreateUser(user.UserName, user.Password, user.Email, user.PasswordQuestion, user.PasswordAnswer, user.IsApproved, null, out createStatus, ...);
if (createStatus == MembershipCreateStatus.Success)
{
return true;
}
else
{ return false;
}
}
[..]
现在在CustomMembership Provider
中public class CustomMembershipProvider : MembershipProvider
{
public IUserRepository UserRepository { get { return DependencyResolver.Current.GetService<IUserRepository>(); } }
public override MembershipUser CreateUser(string username, ...)
{
return UserRepository.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status, Membership.Provider.Name);
}
[...]
现在在控制器
中public AccountController(IAccountRepository accountRepository, IUserRepository userRepository, IUnitOfWork<MyDbContext> unitOfWork)
{
_acountRepository = accountRepository;
_userRepository = userRepository;
UnitOfWork = unitOfWork;
}
public ActionResult Register(UserModel model)
{
if (ModelState.IsValid)
{
_userRepository.RegisterUser(model);
}
}
注意:
1。我从此行return new MembershipUser(providername,...);
的UserRepository CreateUser方法获得错误提供程序名称 Null 。
但我已在 customMembership 提供程序中将其作为Membership.Provider.Name
传递给我。
2. 从控制器调用 Membershipprovider 方法的正确方法是什么?
3。是否需要通过 IUserRepository 调用成员资格提供程序方法的任何其他 Ninject 配置,其他如下?
kernel.Bind<IUserRepository>().To<UserRepository>();
4. .Web.config为previous post
答案 0 :(得分:0)
因此,您有CustomMembershipProvider.CreateUser和UserRepository.CreateUser。 你的控制器直接调用UserRepository.CreateUser,但我想你真的想调用CustomMembershipProvider.CreateUser?
您的控制器正在呼叫
public bool RegisterUser(UserModel user)
{
MembershipCreateStatus createStatus;
CreateUser(user.UserName, user.Password, user.Email, user.PasswordQuestion, user.PasswordAnswer, user.IsApproved, null, out createStatus, ...);
反过来调用
public MembershipUser CreateUser(string username, ... , string providername = null)
但是,据我所知,您正在为提供者名称传递NULL。
您真正希望将其分解为可以注入控制器的MembershipService。
我不能给你一个简单的“改变这一行”的答案,问题是概念性的,对不起。你可以传递一个硬编码的提供者名称但是你确实开始违反所有内容。
答案 1 :(得分:0)
在Web.Config中,这样......你告诉会员提供者和提供者使用。 在这种情况下,您的自定义提供 但我不确定为什么ninject在这个概念中发挥作用,所以无法帮助解决这方面的问题。 客户提供商必须知道自己的名字。但这不是一个问题。 Web.config具有自定义提供程序的名称。所以配置不是硬编码。
<membership defaultProvider="YourProvider" userIsOnlineTimeWindow="5">
<providers>
<!--attribute names in camelCase version of .net class MembershipProvider Properties. http://msdn.microsoft.com/en-us/library/system.web.security.membership_properties
See BOS class for constants : BOSSysConst
type = Implmenting provider class namespace.Classname
How to configure membership providers... http://msdn.microsoft.com/en-us/library/6e9y4s5t -->
<add name="YourProvider"
type="YourNamespace.YourProvider"
connectionStringName="YourConnectionString"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
passwordFormat="Hashed"
applicationName="IMPORTANT" />
</providers>
</membership>
Controller为createUser Membership.CreateUser 调用静态类 或 Membership.ValidateUser 登录。
有关实施自定义提供程序的详细信息,请参阅 http://msdn.microsoft.com/en-us/library/44w5aswa%28v=vs.100%29.aspx
// Sample calling the create user function
// POST: /Account/Register
[AllowAnonymous]
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}