asp.net-mvc-4 – ASP.Net MVC 4和WebSecurity – 通过电子邮件
我正在使用ASP.Net MVC 4项目的默认Internet应用程序模板。帐户控制器中的注册操作是这样的 –
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { WebSecurity.CreateUserAndAccount(model.UserName,model.Password); WebSecurity.Login(model.UserName,model.Password); return RedirectToAction("Index","Home"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("",ErrorCodeToString(e.StatusCode)); } } // If we got this far,something failed,redisplay form return View(model); } 但是,这并没有提供任何可以使用的功能来使用电子邮件地址进行注册,而且步骤可以发送电子邮件以进行确认。帐号激活。 解决方法
实际上SimpleMembership有线支持2步确认。
Go to this blog post查看如何扩展UserProfile以包含电子邮件属性。修改注册视图以捕获电子邮件。现在,当您创建新用户使用
CreateUserAndAccount这样。
string confirmationToken = WebSecurity.CreateUserAndAccount(model.UserName,model.Password,new{Email = model.Email},true); 现在,您只需使用ASP.NET MVC最喜爱的电子邮件方式,向用户发送电子邮件确认信息。我喜欢Postal,因为您可以使用Razor引擎生成电子邮件的正文。电子邮件正文将包含一个指向作为id({controller} / {action} / {id}))的新网页(控制器/操作)的链接。当用户点击链接时,您的代码将使用WebSecurity.ConfirmAccount执行确认。这就是为您的MVC 4应用程序添加电子邮件确认所需要的。有一个step-by-step guide for adding email confirmation to MVC 4 here。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-3 – 安全地通过ASP.NET同步上下文执行任务,而
- asp.net – 处理程序“PageHandlerFactory-ISAPI-4.0_32bit
- asp.net-mvc – 使用Visual Studio和ASP.NET MVC加载时间非
- asp.net-core – 在JsonPatchDocument中使用.Net Core Web
- asp.net – SignalR Autofac OWIN:为什么GlobalHost.Conne
- .net – RESTful WCF的裸最低配置
- asp.net – 具有复合键的Odata v3 Web Api导航
- ASP.NET和C#代码中的常量
- asp.net – 获取Gridview中隐藏列的值
- asp.net – 设置Response.Status生成“HTTP状态字符串无效”