2014-05-06 121 views
1

我希望擴展Visual Studio 2013中新的MVC項目標準的現有帳戶/註冊過程。儘管編輯了我認爲必需的內容,但當我發佈/帳戶/註冊時重定向到登錄頁面。MVC擴展註冊表

我已經修改了AspNetUsers表包括以下額外字段:

[TitleId]  INT   NULL, 
[SexId]   INT   NULL, 
[Forename]  NVARCHAR (50) NULL, 
[Surname]  NVARCHAR (50) NULL, 
[Email]   NVARCHAR (100) NULL, 

我修改了登記表,包括新的領域。

我已經修改ApplicationUser如下:

var user = new ApplicationUser() { UserName = model.UserName, TitleId = model.TitleId, Forename = model.Forename, Surname = model.Surname, Email = model.Email }; 

我已經更新了RegisterViewModel如下:

public class RegisterViewModel 
{ 
    [Display(Name = "Title")] 
    public string TitleId { get; set; } 

    [Required] 
    [StringLength(50, ErrorMessage = "The {0} must be at least {1} characters long.", MinimumLength = 1)] 
    [DataType(DataType.Text)] 
    [Display(Name = "Forename")] 
    public string Forename { get; set; } 

    [Required] 
    [StringLength(50, ErrorMessage = "The {0} must be at least {1} characters long.", MinimumLength = 1)] 
    [DataType(DataType.Text)] 
    [Display(Name = "Surname")] 
    public string Surname { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage = "The {0} must be at least {1} characters long.", MinimumLength = 6)] 
    [DataType(DataType.EmailAddress)] 
    [Display(Name = "Email Address")] 
    public string Email { get; set; } 

    [Required] 
    [StringLength(50, ErrorMessage = "The {0} must be at least {1} characters long.", MinimumLength = 6)] 
    [Display(Name = "Username")] 
    public string UserName { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 

    [DataType(DataType.Password)] 
    [Display(Name = "Confirm password")] 
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 
} 

儘管所有這些變化,註冊頁面將不會顯示,只是重定向和我從Visual Studio 2013重新獲得代碼語法問題或構建錯誤沒有錯誤。

任何幫助,將不勝感激:-)

下面是ActionController.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Security.Claims; 
using System.Threading.Tasks; 
using System.Web; 
using System.Web.Mvc; 
using Microsoft.AspNet.Identity; 
using Microsoft.AspNet.Identity.EntityFramework; 
using Microsoft.Owin.Security; 
using WebApplication1.Models; 

namespace WebApplication1.Controllers 
{ 
    [Authorize] 
    public class AccountController : Controller 
    { 
     private WebApplication1Entities db = new WebApplication1Entities(); 

     public AccountController() 
      : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))) 
     { 
     } 

     public AccountController(UserManager<ApplicationUser> userManager) 
     { 
      UserManager = userManager; 
      var userValidator = UserManager.UserValidator as UserValidator<ApplicationUser>; 
      userValidator.AllowOnlyAlphanumericUserNames = false; 
     } 

     public UserManager<ApplicationUser> UserManager { get; private set; } 

     // 
     // GET: /Account/Login 
     [AllowAnonymous] 
     public ActionResult Login(string returnUrl) 
     { 
      ViewBag.ReturnUrl = returnUrl; 
      return View(); 
     } 

     // 
     // POST: /Account/Login 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = await UserManager.FindAsync(model.Email, model.Password); 
       if (user != null) 
       { 
        await SignInAsync(user, model.RememberMe); 
        return RedirectToLocal(returnUrl); 
       } 
       else 
       { 
        ModelState.AddModelError("", "Invalid Email or password."); 
       } 
      } 

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

     [ChildActionOnly] 
     public ActionResult Title() 
     { 
      var titleModel = from m in db.Titles select m; 
      return View(titleModel); 
     } 
     // 
     // GET: /Account/Register 
     [AllowAnonymous] 
     public ActionResult Register() 
     { 
      return View(); 
     } 

     // 
     // POST: /Account/Register 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Register(RegisterViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = new ApplicationUser() { UserName = model.UserName, TitleId = model.TitleId, Forename = model.Forename, Surname = model.Surname, Email = model.Email }; 
       var result = await UserManager.CreateAsync(user, model.Password); 
       if (result.Succeeded) 
       { 
        await SignInAsync(user, isPersistent: false); 
        return RedirectToAction("Index", "Home"); 
       } 
       else 
       { 
        AddErrors(result); 
       } 
      } 

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

     // 
     // POST: /Account/Disassociate 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Disassociate(string loginProvider, string providerKey) 
     { 
      ManageMessageId? message = null; 
      IdentityResult result = await UserManager.RemoveLoginAsync(User.Identity.GetUserId(), new UserLoginInfo(loginProvider, providerKey)); 
      if (result.Succeeded) 
      { 
       message = ManageMessageId.RemoveLoginSuccess; 
      } 
      else 
      { 
       message = ManageMessageId.Error; 
      } 
      return RedirectToAction("Manage", new { Message = message }); 
     } 

     // 
     // GET: /Account/Manage 
     public ActionResult Manage(ManageMessageId? message) 
     { 
      ViewBag.StatusMessage = 
       message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." 
       : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set." 
       : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed." 
       : message == ManageMessageId.Error ? "An error has occurred." 
       : ""; 
      ViewBag.HasLocalPassword = HasPassword(); 
      ViewBag.ReturnUrl = Url.Action("Manage"); 
      return View(); 
     } 

     // 
     // POST: /Account/Manage 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Manage(ManageUserViewModel model) 
     { 
      bool hasPassword = HasPassword(); 
      ViewBag.HasLocalPassword = hasPassword; 
      ViewBag.ReturnUrl = Url.Action("Manage"); 
      if (hasPassword) 
      { 
       if (ModelState.IsValid) 
       { 
        IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword); 
        if (result.Succeeded) 
        { 
         return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess }); 
        } 
        else 
        { 
         AddErrors(result); 
        } 
       } 
      } 
      else 
      { 
       // User does not have a password so remove any validation errors caused by a missing OldPassword field 
       ModelState state = ModelState["OldPassword"]; 
       if (state != null) 
       { 
        state.Errors.Clear(); 
       } 

       if (ModelState.IsValid) 
       { 
        IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword); 
        if (result.Succeeded) 
        { 
         return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess }); 
        } 
        else 
        { 
         AddErrors(result); 
        } 
       } 
      } 

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

     // 
     // POST: /Account/ExternalLogin 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public ActionResult ExternalLogin(string provider, string returnUrl) 
     { 
      // Request a redirect to the external login provider 
      return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl })); 
     } 

     // 
     // GET: /Account/ExternalLoginCallback 
     [AllowAnonymous] 
     public async Task<ActionResult> ExternalLoginCallback(string returnUrl) 
     { 
      var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); 
      if (loginInfo == null) 
      { 
       return RedirectToAction("Login"); 
      } 

      // Sign in the user with this external login provider if the user already has a login 
      var user = await UserManager.FindAsync(loginInfo.Login); 
      if (user != null) 
      { 
       await SignInAsync(user, isPersistent: false); 
       return RedirectToLocal(returnUrl); 
      } 
      else 
      { 
       // If the user does not have an account, then prompt the user to create an account 
       ViewBag.ReturnUrl = returnUrl; 
       ViewBag.LoginProvider = loginInfo.Login.LoginProvider; 
       return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { UserName = loginInfo.DefaultUserName }); 
      } 
     } 

     // 
     // POST: /Account/LinkLogin 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult LinkLogin(string provider) 
     { 
      // Request a redirect to the external login provider to link a login for the current user 
      return new ChallengeResult(provider, Url.Action("LinkLoginCallback", "Account"), User.Identity.GetUserId()); 
     } 

     // 
     // GET: /Account/LinkLoginCallback 
     public async Task<ActionResult> LinkLoginCallback() 
     { 
      var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(XsrfKey, User.Identity.GetUserId()); 
      if (loginInfo == null) 
      { 
       return RedirectToAction("Manage", new { Message = ManageMessageId.Error }); 
      } 
      var result = await UserManager.AddLoginAsync(User.Identity.GetUserId(), loginInfo.Login); 
      if (result.Succeeded) 
      { 
       return RedirectToAction("Manage"); 
      } 
      return RedirectToAction("Manage", new { Message = ManageMessageId.Error }); 
     } 

     // 
     // POST: /Account/ExternalLoginConfirmation 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) 
     { 
      if (User.Identity.IsAuthenticated) 
      { 
       return RedirectToAction("Manage"); 
      } 

      if (ModelState.IsValid) 
      { 
       // Get the information about the user from the external login provider 
       var info = await AuthenticationManager.GetExternalLoginInfoAsync(); 
       if (info == null) 
       { 
        return View("ExternalLoginFailure"); 
       } 
       var user = new ApplicationUser() { UserName = model.UserName }; 
       var result = await UserManager.CreateAsync(user); 
       if (result.Succeeded) 
       { 
        result = await UserManager.AddLoginAsync(user.Id, info.Login); 
        if (result.Succeeded) 
        { 
         await SignInAsync(user, isPersistent: false); 
         return RedirectToLocal(returnUrl); 
        } 
       } 
       AddErrors(result); 
      } 

      ViewBag.ReturnUrl = returnUrl; 
      return View(model); 
     } 

     // 
     // POST: /Account/LogOff 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult LogOff() 
     { 
      AuthenticationManager.SignOut(); 
      return RedirectToAction("Index", "Home"); 
     } 

     // 
     // GET: /Account/ExternalLoginFailure 
     [AllowAnonymous] 
     public ActionResult ExternalLoginFailure() 
     { 
      return View(); 
     } 

     [ChildActionOnly] 
     public ActionResult RemoveAccountList() 
     { 
      var linkedAccounts = UserManager.GetLogins(User.Identity.GetUserId()); 
      ViewBag.ShowRemoveButton = HasPassword() || linkedAccounts.Count > 1; 
      return (ActionResult)PartialView("_RemoveAccountPartial", linkedAccounts); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing && UserManager != null) 
      { 
       UserManager.Dispose(); 
       UserManager = null; 
      } 
      base.Dispose(disposing); 
     } 

     #region Helpers 
     // Used for XSRF protection when adding external logins 
     private const string XsrfKey = "XsrfId"; 

     private IAuthenticationManager AuthenticationManager 
     { 
      get 
      { 
       return HttpContext.GetOwinContext().Authentication; 
      } 
     } 

     private async Task SignInAsync(ApplicationUser user, bool isPersistent) 
     { 
      AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); 
      var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); 
      AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); 
     } 

     private void AddErrors(IdentityResult result) 
     { 
      foreach (var error in result.Errors) 
      { 
       ModelState.AddModelError("", error); 
      } 
     } 

     private bool HasPassword() 
     { 
      var user = UserManager.FindById(User.Identity.GetUserId()); 
      if (user != null) 
      { 
       return user.PasswordHash != null; 
      } 
      return false; 
     } 

     public enum ManageMessageId 
     { 
      ChangePasswordSuccess, 
      SetPasswordSuccess, 
      RemoveLoginSuccess, 
      Error 
     } 

     private ActionResult RedirectToLocal(string returnUrl) 
     { 
      if (Url.IsLocalUrl(returnUrl)) 
      { 
       return Redirect(returnUrl); 
      } 
      else 
      { 
       return RedirectToAction("Index", "Home"); 
      } 
     } 

     private class ChallengeResult : HttpUnauthorizedResult 
     { 
      public ChallengeResult(string provider, string redirectUri) : this(provider, redirectUri, null) 
      { 
      } 

      public ChallengeResult(string provider, string redirectUri, string userId) 
      { 
       LoginProvider = provider; 
       RedirectUri = redirectUri; 
       UserId = userId; 
      } 

      public string LoginProvider { get; set; } 
      public string RedirectUri { get; set; } 
      public string UserId { get; set; } 

      public override void ExecuteResult(ControllerContext context) 
      { 
       var properties = new AuthenticationProperties() { RedirectUri = RedirectUri }; 
       if (UserId != null) 
       { 
        properties.Dictionary[XsrfKey] = UserId; 
       } 
       context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider); 
      } 
     } 
     #endregion 
    } 
} 
+0

您是否更新過.chtml文件? – qamar

+0

你指的是哪個chtml文件? – iggyweb

+0

我認爲registration.chtml。在帳戶控制器中檢查註冊操作。 – qamar

回答

0

這聽起來像它期待的用戶進行身份驗證,當用戶還沒有通過身份驗證。所以這裏可能需要考慮安全問題。確保您使用[AllowAnonymous]以允許匿名用戶進入系統。或者它檢查安全性的方式,它沒有正確檢查,因此你的用戶沒有得到認證。

我不知道所做的所有更改以及開箱即用的情況,但這聽起來像是根據您的描述進行的。

+0

我剝去了一些東西,事實證明它是導致問題的TitleId字段,出於某種原因導致數據類型錯誤,但我需要將TitleId選擇字段放入形式從標題表返回一個Id和標題的列表,任何想法? – iggyweb