我想用一个模型来创建一个用户使用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?}");
});
}
是否有身体有一个想法可能是错误的。
的可能的复制[什么是一个NullReferenceException,我该如何解决它?](https:// stackoverflow。com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Hintham
情况并非如此这是一个模型问题 – roguedb593