首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何扩展/定制MVC4互联网应用程序网络安全/简单成员关系

如何扩展/定制MVC4互联网应用程序网络安全/简单成员关系
EN

Stack Overflow用户
提问于 2013-01-24 00:58:17
回答 1查看 4.1K关注 0票数 3

我一直在尽力搜索有关如何在Visual Studio2012学习版的MVC4互联网应用程序(EF 5代码优先)中修改/扩展/定制默认会员系统的更多信息。

我想知道如何实现电子邮件验证,以便当用户注册时,发送一封带有激活链接的电子邮件。当他们点击链接时,他们的账户被激活,他们可以使用自己的用户名或电子邮件登录。

我还想知道如何通过在注册过程中分配默认角色来实现注册用户的简单角色。

类似的问题:How do I manage profiles using SimpleMembership?

How do you extend the SimpleMembership authentication in ASP.NET MVC4

但我真的很想使用现有的简单会员制度。

这篇文章非常接近:http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/

我也看过这篇文章:http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx

到目前为止,这是我找到的最接近的:http://weblogs.asp.net/thangchung/archive/2012/11/15/customize-the-simplemembership-in-asp-net-mvc-4-0.aspx

这对于WebPages也很有用:http://blog.osbornm.com/archive/2010/07/21/using-simplemembership-with-asp.net-webpages.aspx

我希望找到一个更全面的演练来扩展它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-30 23:09:44

看起来你没有得到任何答案。

除非我不完全了解您想要做什么,否则不需要修改/扩展/自定义默认SimpleMembership来提供电子邮件注册机制,或者在注册过程中分配默认角色,因为所有这些都可以在AccountController中完成。

作为示例,下面是我正在使用的register方法:

代码语言:javascript
复制
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid) //TODO Change this to use a worker to send emails.
        {
            // Check if email exists already before creating new user
            using (UsersContext db = new UsersContext())
            {
                UserProfile email = db.UserProfiles.FirstOrDefault(u => u.Email.ToLower() == model.Email.ToLower());
                UserProfile uName =
                    db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());

                // Attempt to register the user
                try
                {
                    if (email == null && uName == null && this.IsCaptchaVerify("Captcha is not valid"))
                    {
                        bool requireEmailConfirmation = !WebMail.SmtpServer.IsEmpty();
                        string confirmationToken = WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new
                        {
                            FirstName = model.FirstName,
                            LastName = model.LastName,
                            Email = model.Email                               
                        },
                        requireEmailConfirmation);
                        if (requireEmailConfirmation)
                        {
                            EmailViewModel eml = new EmailViewModel
                                                     {
                                                         ToEmail = model.Email,
                                                         Subject = "Confirmez votre inscription",
                                                         FirstName = model.FirstName,
                                                         LastName = model.LastName,
                                                         Body = confirmationToken
                                                     };

                            UserMailer.ConfirmRegistration(eml).SendAsync();

                            Response.Redirect("~/Account/Thanks");                            
                        }
                        else
                        {
                            WebSecurity.Login(model.UserName, model.Password);
                            Response.Redirect("~/");
                        }                           
                    }
                    else
                    {
                        if (email != null)
                            ModelState.AddModelError("Email", "Email address already exists. Please enter a different email address.");

                        if (uName != null)
                            ModelState.AddModelError("UserName", "User Name already exists. Please enter a different user name.");

                        if (!this.IsCaptchaVerify("Captcha is not valid"))
                            TempData["ErrorMessage"] = "Captcha is not valid";
                    }

                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

这里没有分配默认角色,但是一旦验证了EmailConfirmation,就很容易添加它。

因为这个问题已经很老了,我希望它能帮助到一些人!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14485146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档