如果是我,我可能会这样做:
首先,假设您正在使用带有Entity Framework或一些数据库连接(ADO,LINQ to SQL等)的SimpleMembership,您将有两个组件:
WebSecurity.*
方法调用和数据库连接以进行配置文件更改。个人而言,我会向数据库添加
CONSTRAINT
以确保您的数据是纯净的,但您也可以实现处理此逻辑的成员资格服务。
其次,将它们分组到一个接口中,该接口可以在控制器中引用(例如以下内容):
public interface IMembershipService
{
Int32 CurrentUserId { get; }
String CurrentUserName { get; }
Boolean IsAuthenticated { get; }
Boolean CreateUserAndAccount(String username, String password, String emailaddress = null);
Boolean CreateUserAndAccount(String username, string password, out String confirmationToken, String emailaddress = null);
Boolean Login(String username, String password, Boolean persistCookie = false);
void Logout();
}
然后,您可以将服务实现为SimpleMembership和数据库连接的混合体。为了保持通用性,我使用了模式,但这也可以是直接的DbContext
,ObjectContext
等。我还保持简短,所以请原谅缺少校验和短实现。
public class MembershipService : IMembershipService
{
protected readonly SimpleMembershipProvider membershiProvider;
protected readonly SimpleRoleProvider roleProvider;
protected readonly IRepository<UserProfile> profileRepository;
public MembershipService(IRepository<UserProfile> profileRepository)
{
this.membershipProvider = Membership.Provider as SimpleMembershipProvider;
this.roleProvider = Role.Provider as SimpleRoleProvider;
this.profileRepository = userRepository;
}
#region IMembershipService Implementation
public Int32 CurrentUserId
{
get { return WebSecurity.CurrentUserId; }
}
public String CurrentUserName
{
get { return WebSecurity.CurrentUserName; }
}
public Boolean IsAuthenticated
{
get { return WebSecurity.IsAuthenticated; }
}
public Boolean CreateUserAndAccount(String username, String password, String emailaddress = null)
{
if (!this.profileRepository.Any(x => x.EmailAddress == emailaddress))
{
WebSecurity.CreateUserAndAccount(username, password, new
{
EmailAddress = emailaddress
}, createConfirmationToken);
return true;
}
else
{
return false;
}
}
public Boolean CreateUserAndAccount(String username, String password, out String confirmationToken, String emailaddress = null, out)
{
if (this.profileRepository.First(x => x.EmailAddress == emailaddress) == null)
{
confirmationToken = WebSecurity.CreateUserAndAccount(username, password, new
{
EmailAddress = emailaddress
}, createConfirmationToken);
return true;
}
else
{
confirmationToken = String.Empty;
return false;
}
}
public Boolean Login(String username, String password, Boolean persistCookie = false)
{
return WebSecurity.Login(username, password, persistCookie);
}
public void Logout()
{
WebSecurity.Logout();
}
#endregion
}
现在您可以在控制器中引用此接口,并将逻辑放在一个地方。如果您使用 DI 容器,请注册它,但这里是一个示例实现:
public class AccountController: Controller
{
private readonly IMembershipService membershipService;
public AccountController(IMembershipService membershipService)
{
this.membershipService = membershipService;
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Register(LoginViewModel model, String returnUrl)
{
if (ModelState.IsValid)
{
if (this.membershipService.CreateUserandAccount(model.Username, model.Password, model.EmailAddress))
{
this.membershipService.Login(model.Username, model.Password);
if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToRoute("Default");
}
else
{
ModelState.AddModelError("", "Unable to register.");
}
}
return View(model);
}
}
如果您正在使用EntityFramework,您也可以使用
IValidatableObject
。为了避免重复,这里有另一个SO问题/答案来检查唯一条目:
Entity Framework IValidatableObject