2013-01-23 28 views
3

我一直在盡力搜索有關如何修改/擴展/自定義MVC4 Internet應用程序(EF 5 Code First)中默認成員系統的更多信息。 Visual Studio 2012 Express。如何擴展/定製MVC4互聯網應用程序WebSecurity/SimpleMembership

我想知道如何實現電子郵件驗證,以便在用戶註冊電子郵件時通過激活鏈接發送。當他們點擊鏈接時,他們的帳戶被激活,他們可以使用他們的用戶名或電子郵件登錄。

我也想知道如何通過在註冊過程中分配默認角色爲註冊用戶實現簡單的角色。

類似的問題: How do I manage profiles using SimpleMembership?

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

但我真的很想與現有simplemembership系統的正常工作。

這個職位是非常接近: 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

這也是有用但網頁: http://blog.osbornm.com/archive/2010/07/21/using-simplemembership-with-asp.net-webpages.aspx

我希望找到一個更全面地介紹如何正確擴展它。

+0

你還需要這個問題的答案嗎? – Komengem

回答

1

它看起來不像你有任何答案。

除非我不完全明白你想要做什麼,否則不需要修改/擴展/自定義默認的SimpleMembership來提供一個電子郵件註冊機制,或者在註冊過程中分配一個默認角色,因爲所有這些都可以在AccountController中完成。

作爲一個例子,這裏是一個註冊方法我使用:

[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驗證補充。

由於問題相當老,我希望它可以幫助別人!

相關問題