2016-04-14 100 views
0

我完全在ASP.NET上做菜,我試圖用這種技術創建我的第一個Web應用程序。MVC5 ApplicationUser自定義值

我正在使用MVC模板並適應我的項目。

我的第一個目標是創建一個登錄系統(不只是使用模板,我想了解它)。

目前,我試圖添加具有以下屬性的用戶:First NameLast NameEmail,Password

但我不能使用我自己的屬性ApplicationUser(idk爲什麼)。

你能告訴我,在noob語言,我怎麼做?

正如我所說的,我使用的模板,但我會將代碼:

AccountViewModels.cs

using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 

namespace LoginSystem.Models 
{ 

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

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

     [Required] 
     [EmailAddress] 
     [Display(Name = "Email")] 
     public string Email { 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; } 
    } 
} 

AccountController.cs

using System; 
using System.Globalization; 
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.Owin; 
using Microsoft.Owin.Security; 
using LoginSystem.Models; 

namespace LoginSystem.Controllers 
{ 
    [Authorize] 
    public class AccountController : Controller 
    { 
     private ApplicationSignInManager _signInManager; 
     private ApplicationUserManager _userManager; 

     public AccountController() 
     { 
     } 

     public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager) 
     { 
      UserManager = userManager; 
      SignInManager = signInManager; 
     } 

     public ApplicationSignInManager SignInManager 
     { 
      get 
      { 
       return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>(); 
      } 
      private set 
      { 
       _signInManager = value; 
      } 
     } 

     public ApplicationUserManager UserManager 
     { 
      get 
      { 
       return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
      } 
      private set 
      { 
       _userManager = value; 
      } 
     } 


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

     // 
     // POST: /Account/Register 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Register(AddUserViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; 
       var result = await UserManager.CreateAsync(user, model.Password); 
       if (result.Succeeded) 
       { 
        await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 

        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
        // Send an email with this link 
        // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
        // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
        // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 

        return RedirectToAction("Index", "Home"); 
       } 
       AddErrors(result); 
      } 

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


    } 
} 

IdentifyModels

using System.Data.Entity; 
using System.Security.Claims; 
using System.Threading.Tasks; 
using Microsoft.AspNet.Identity; 
using Microsoft.AspNet.Identity.EntityFramework; 

namespace LoginSystem.Models 
{ 
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more. 
    public class ApplicationUser : IdentityUser 
    { 
     public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
     { 
      // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
      var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
      // Add custom user claims here 
      return userIdentity; 
     } 
    } 

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
    { 
     public ApplicationDbContext() 
      : base("DefaultConnection", throwIfV1Schema: false) 
     { 
     } 

     public static ApplicationDbContext Create() 
     { 
      return new ApplicationDbContext(); 
     } 
    } 
} 

AddUser.cshtml

@model LoginSystem.Models.AddUserViewModel 
@{ 
    ViewBag.Title = "Add User"; 
} 

<h2>@ViewBag.Title.</h2> 

@using (Html.BeginForm("Add User", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
{ 
    @Html.AntiForgeryToken() 
    <h4>Add a new user.</h4> 
    <hr /> 
    @Html.ValidationSummary("", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" class="btn btn-default" value="Register" /> 
     </div> 
    </div> 
} 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

TiA!

+0

爲了幫助我們,您需要發佈您的控制器操作和登錄系統的型號 –

+0

您正在使用身份? – Randrade

+0

編輯:添加代碼 –

回答

0

如果要在ApplicationUser.cs類中使用自定義屬性,請添加所需屬性並進行遷移,請使用signInManager在註冊表的後置操作中生成Cookie,而不是通過方法將其直接放到ApplicationUser類中。

嘗試使用身份屬性,如果他們滿足您的需求,以避免不同的遷移。

相關問題