- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要关于该怎么做的建议。我目前正在使用 WebSecurity 方法来完成所有与帐户相关的工作。但是它不支持电子邮件唯一性验证,所以我有几个选择:
编写(子类)一个新的 SimpleMembershipProvider 来覆盖现有的 createuserAndAccount 方法来验证电子邮件地址。但我还必须实现登录/注销功能(就像网络安全一样)和其他一些功能。
在数据库上添加唯一性约束并在我的代码中捕获它们。然而,这会导致我依赖数据库。
这可能有点便宜,但我可以将 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
。为了避免重复,这是另一个检查唯一条目的问题/答案:
关于asp.net-mvc - Asp mvc 4 成员资格和 Web 安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16020557/
我正在为客户构建一个简单的 Django 应用程序,它允许用户注册付费帐户以访问网站的某些部分;但是,除了几个简单的 Paypal 按钮之外,我从未处理过付款处理。 我的问题是,在这种情况下我该如何安
根据 ISO C 标准 (6.3.16.1),如果“两个操作数都是指向兼容类型的合格或非合格版本的指针,并且左边指向的类型具有所有右边指向的类型的限定符”。我在我的静态分析工具中收到了警告,但我不确定
我用 paypal 创建了 2 个订阅按钮。此按钮适用于不同的成员(member)资格:1. 基本 2. 专业。我正在尝试使用此按钮订阅成员(member)资格并且工作没有任何问题。但是当我试图通过其
如果我想做一个管理功能,比如删除 asp.net mvc 示例附带的 asp.net 成员(member)资格内容中的用户。 我尝试查看表格,发现有多个表格添加了行。我想一定有一个更简单的方法。 最佳
我希望在新网站上创建博客和论坛。我希望用户注册后可以在论坛上发帖,在博客上发表评论,并拥有自己的公共(public)个人资料页面。 我有使用 BlogEngine.NET 的经验,所以希望使用它。我目
我对 MVC 和使用 ASP.NET MVC 4 开发应用程序还很陌生。对于身份验证,我在域中使用 Windows 身份验证和 Active Directory。下一步是授权用户(不使用成员(memb
我试图找出用户第一次使用asp.net 成员(member)资格(mysql)登录,并根据它显示一些协议(protocol)页面。我遵循的程序是: When a user registers a ma
我最近刚刚研究出如何使用 PayPal IPN 在用户支付一定金额后启动高级成员(member)资格。一旦用户点击升级帐户,他们就会被定向到 PayPal 进行支付,然后 IPN 就会启动更新数据库以
我正在尝试学习 Java 中的继承和接口(interface)。我有三个不同的类:Customer、SilverCustomer 和 GoldCustomer。 SilverCustomer 和 Go
我刚刚收到一封来自 Apple 开发者门户的电子邮件(2 月 20 日),说我需要续订我的成员(member)资格。当我查看我的帐户时,我的订阅实际上在 4 月 20 日用完。我的问题是,如果我现在续
要向 iOS 应用程序添加推送通知,您是否需要拥有已注册 iOS 成员(member)计划的苹果开发者帐户? 最佳答案 Yes , it is - see the terms here 看这里- In
protected void Button2_Click(object sender, System.EventArgs e) //logout { if (System.Web.HttpCo
我正在构建一个使用蓝牙与远程传感器通信的 iPhone 应用程序。外设是蓝牙自行车速度传感器。该应用程序使用来自外围设备的信息来计算行驶距离。 我看到有一个蓝牙特别兴趣小组 (SIG) 负责确保制造商
我正在使用 asp.net mvc 示例应用程序,并对其进行了一些扩展。我使用 asp.net 成员(member)身份进行用户登录和注册。 我现在想更改它,以便当人们注册时,它不会立即能够登录,而是
我为我的客户创建了一个成员(member)登录系统,现在他们不希望用户在创建新密码时使用他最后 5 个密码之一。 这是内置的东西吗?我怎样才能实现它? 最佳答案 asp.net 成员(member)登
我正在为一个公共(public)站点开发 ASP.NET4.0/C# 应用程序,该应用程序仅需要对在关联企业工作的员工进行身份验证。这个想法是让网站拥有一个 CMS,以便员工可以进入并更改某些内容,而
如何通过 ID 获取用户?MembershipUser mu=Membership.GetUser("用户名");但是,我想通过 ID 获取用户,而不是通过名称获取用户。 最佳答案 Membershi
是否可以让用户拥有多个资格之一才能从事 HIT。例如: qualifications = Qualifications() qualifications.add( Requirement(co
现在我们有几个数据库,每个数据库都有自己的 aspnet 成员表。我们希望将所有成员集中到一个数据库中。例如一个主登录页面,他们将在其中指定数据库、用户名和密码。然后我们将检查集中式数据库并根据他们在
我正在使用 Paypal 的定期成员(member)付款。我想知道是否有成员(member)手动从paypal取消成员(member)资格,我如何从paypal获取返回值/URL,以便我可以跟踪成员(
我是一名优秀的程序员,十分优秀!