2017-10-09 98 views
0

我想用一個模型來創建一個用戶使用asp.net核心2 sql服務器和身份核心。但是我有一個註冊用戶的問題,下面是我的代碼和它產生的錯誤。我希望有一個人可以幫助我。身份核心2問題與註冊

這不僅僅是一個空問題,它是發佈表單時沒有得到正確模型的問題,所以人員間的錯誤是不正確的。

型號:

public class Register 
{ 

    [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; } 
} 

,而我已經打上了post命令

[HttpGet] 
[AllowAnonymous] 
public IActionResult Register(string returnUrl = null) 
{ 
     ViewData["ReturnUrl"] = returnUrl; 
     return View(); 
} 

// 
// POST: /Account/Register 
[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public async Task<IActionResult> Register(AppUser model, string returnUrl = null) 
{ 
     ViewData["ReturnUrl"] = returnUrl; 

      var user = new AppUser { UserName = model.Email, Email = model.Email }; 
      var result = await _userManager.CreateAsync(user, model.Password); 
      if (result.Succeeded) 
      { 
       // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713 
       // Send an email with this link 
       //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); 
       //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); 
       //await _emailSender.SendEmailAsync(model.Email, "Confirm your account", 
       // "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"); 
       await _signInManager.SignInAsync(user, isPersistent: false); 
        return RedirectToLocal(returnUrl); 
      } 



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

這是我的形式

@model solitude.models.Register 
@{ 
    ViewData["Title"] = "Register"; 
    Layout = "~/Views/Shared/_LoginAdminLte.cshtml"; 
} 



<body class="hold-transition register-page"> 
<div class="register-box"> 
    <div class="register-logo"> 
     <a href="../../index2.html"><b>Register</b></a> 
    </div> 
    <div class="register-box-body"> 
     <p class="login-box-msg">Register a new membership</p> 
     <form asp-controller="Account" asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal" role="form"> 

      <div class="form-group has-feedback"> 
       <input type="text" class="form-control" placeholder="Full name"> 


       <span class="glyphicon glyphicon-user form-control-feedback"></span> 
      </div> 
      <div class="form-group has-feedback"> 

       <input asp-for="Email" class="form-control" placeholder="Email"> 

       <span class="glyphicon glyphicon-envelope form-control-feedback"></span> 
      </div> 
      <div class="form-group has-feedback"> 
       <input asp-for="Password" class="form-control" /> 

       <span class="glyphicon glyphicon-lock form-control-feedback"></span> 
      </div> 
      <div class="form-group has-feedback"> 
       <input asp-for="ConfirmPassword" class="form-control" /> 

       <span class="glyphicon glyphicon-log-in form-control-feedback"></span> 
      </div> 
      <div class="row"> 
       <div class="col-xs-8"> 
        <div class="checkbox icheck"> 
         <label> 
          <input type="checkbox"> I agree to the <a href="#">terms</a> 
         </label> 
        </div> 
       </div> 
       <!-- /.col --> 
       <div class="col-xs-4"> 
        <button type="submit" class="btn btn-primary btn-block btn-flat">Register</button> 
       </div> 
       <!-- /.col --> 
      </div> 
     </form> 

HTML寄存器的動作,但我有以下錯誤問題

System.NullReferenceException:對象引用未設置爲對象的實例 。在 solitude.admin.core.Controllers.AccountController.d__6.MoveNext() 在 C:\項目\ solitudeec2core \ solitude.admin.core \ solitude.admin.core \控制器\ AccountController.cs:線 - - 從拋出異常的上一個位置結束堆棧跟蹤---在System.Runtime的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務 任務)處結束 。 CompilerServices.TaskAwaiter`1.GetResult() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext() ---從以前位置拋出異常的堆棧跟蹤結束---在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在

當我看行88它顯示了這一點,但我不明白爲什麼我有這個問題

VAR的結果=等待_userManager .CreateAsync(user,model.Password);

我Startup.cs

// This method gets called by the runtime. Use this method to add services to the container. 
    public void ConfigureServices(IServiceCollection services) 
    { 
     var connection = @"Server=----SEVER NAME HIDDEN---;Database=solitude;Trusted_Connection=True;"; 

     services.AddDbContext<SolitudeDBContext>(options => options.UseSqlServer(connection)); 
     services.AddIdentity<AppUser, IdentityRole>() 
     .AddEntityFrameworkStores<SolitudeDBContext>() 
     .AddDefaultTokenProviders(); 

     services.AddMvc(); 

    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
    { 
     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
      app.UseBrowserLink(); 
     } 
     else 
     { 
      app.UseExceptionHandler("/Home/Error"); 
     } 

     app.UseStaticFiles(); 
     app.UseAuthentication(); 
     app.UseMvc(routes => 
     { 
      routes.MapRoute(
       name: "default", 
       template: "{controller=Home}/{action=Index}/{id?}"); 
     }); 
    } 

是否有身體有一個想法可能是錯誤的。

+0

的可能的複製[什麼是一個NullReferenceException,我該如何解決它?](https:// stackoverflow。com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Hintham

+0

情況並非如此這是一個模型問題 – roguedb593

回答

0

的錯誤是在這條線:

public async Task<IActionResult> Register(AppUser model, string returnUrl = null) 

你應該提供註冊類型作爲模型輸入參數,而不是APPUSER

public async Task<IActionResult> Register(Register model, string returnUrl = null) 
+0

我這樣做了,但我仍然得到相同的結果錯誤 – roguedb593

+0

嗯...這應該工作。你的AppUser繼承自IdentityUser?您是否對AppUser進行了一些更改? – OctoCode