gpt4 book ai didi

asp.net-mvc - Asp mvc 4 成员资格和 Web 安全

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

我需要关于该怎么做的建议。我目前正在使用 WebSecurity 方法来完成所有与帐户相关的工作。但是它不支持电子邮件唯一性验证,所以我有几个选择:

  1. 编写(子类)一个新的 SimpleMembershipProvider 来覆盖现有的 createuserAndAccount 方法来验证电子邮件地址。但我还必须实现登录/注销功能(就像网络安全一样)和其他一些功能。

  2. 在数据库上添加唯一性约束并在我的代码中捕获它们。然而,这会导致我依赖数据库。

  3. 这可能有点便宜,但我可以将 WebSecurity 源代码(自开放以来)复制/粘贴到新类上,并修改 createUserAndAccount 方法。

还有其他选择吗?我目前的目标是选项3,这将是最快的方法。顺便说一句,将来我也将需要角色,并且我不确定 WebSecurity 是否为它们提供支持。

最佳答案

如果是我,我可能会采用以下方式:

首先,假设您将 SimpleMembership 与 Entity Framework 或某些数据库连接(ADO、LINQ to SQL 等)一起使用,您将拥有两个组件: WebSecurity.*方法调用和数据库连接以进行配置文件更改。就我个人而言,我会添加 CONSTRAINT到数据库以确保您的数据是纯净的,但您也可以实现处理此逻辑的成员(member)服务。

首先,将它们分组到一个可以在 Controller 中引用的接口(interface)(如下所示):

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 和数据库连接的混合体。为了保持通用,我使用 IRepository<T>模式,但这可能是直接 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)
{
// validate the email address is unique
if (!this.profileRepository.Any(x => x.EmailAddress == emailaddress))
{
WebSecurity.CreateUserAndAccount(username, password, new
{
EmailAddress = emailaddress
}, createConfirmationToken);
return true;
}
else
{
// handle the error how you see fit
// (maybe even exception?)
return false;
}
}
public Boolean CreateUserAndAccount(String username, String password, out String confirmationToken, String emailaddress = null, out)
{
// validate the email address is unique
if (this.profileRepository.First(x => x.EmailAddress == emailaddress) == null)
{
confirmationToken = WebSecurity.CreateUserAndAccount(username, password, new
{
EmailAddress = emailaddress
}, createConfirmationToken);
return true;
}
else
{
// handle the error how you see fit
// (maybe even exception?)
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
}

现在您可以在 Controller 中引用此接口(interface)并将逻辑放在一处。如果您使用 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 。为了避免重复,这是另一个检查唯一条目的问题/答案:

Entity Framework IValidatableObject

关于asp.net-mvc - Asp mvc 4 成员资格和 Web 安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16020557/

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