UPDATE1問題は、登録フォームにカスタムフィールド(名前と姓)を作成したことです。私はこれらのフィールドを削除し、すべてが再び働いた。今度は、これらのフィールドをもう一度追加してすべてを機能させる方法を見つける必要があります。ASP.Netは登録ユーザーとログインできません
まず最初に、私はネイティブ英語の講師ではないので、間違って申し訳ありません、私はC#、ASP.NET、MVC、htmlのプログラミングの初心者です質問:愚かな、再び申し訳ありません:)
とにかく、私はあなたのログインと登録メカニズムを含むいくつかの基本的なものを与えるVisual StudioのMVC5テンプレートから私のプロジェクトを開始しました。これを私のローカルデータベースにリンクして、テストアカウントを登録してログに記録し直して、すべて正常に機能しました。それから私は、私のウェブサイトにすばらしい外観を与えるために非常に素晴らしいテンプレートをオンラインで見つけました。私はこれをテンプレートに実装し始めました。これはすべて正常に動作しますが、私は奇妙な問題につまずいた...
私は新しいアカウントを登録することができます(私はデータベースに新しいアカウントが表示され、私は自動的にログインしています)。しかし、ログオフしてログインページから再度ログインしようとすると、ログインが無効でアカウントが存在しないと表示されます。これを引き起こした原因は何ですか?私は遠くに捜索したが、一つの答えが私の問題を助けたわけではなかった。前もって感謝します。
これは_layoutで私が使用_LoginPartial.cshtmlファイルこれはAccountController
[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)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
でログインMethodeのある
@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
@Html.AntiForgeryToken()
<ul class="member-actions">
<li>
@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
}
}
else
{
<ul class="member-actions">
<li><a href="@Url.Action("Login","Account")" class="login">Log in</a></li>
<li><a href="@Url.Action("Register","Account")" class="btn-white btn-small">Registreer</a></li>
</ul>
}
ファイルLogin.cshtmlページ
からのコードです@using Toverpoort.Models
@model LoginViewModel
@{
ViewBag.Title = "Log in";
}
<h2>@ViewBag.Title.</h2>
<div class="row">
<div class="col-md-8">
<section id="loginForm">
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Use a local account to log in.</h4>
<hr />
@Html.ValidationSummary(true, "", 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" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
</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" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
<p>
@Html.ActionLink("Register as a new user", "Register")
</p>
@* Enable this once you have account confirmation enabled for password reset functionality
<p>
@Html.ActionLink("Forgot your password?", "ForgotPassword")
</p>*@
}
</section>
</div>
<div class="col-md-4">
<section id="socialLoginForm">
@Html.Partial("_ExternalLoginsListPartial", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
</section>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
登録方法
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.Voornaam + " " + model.Achternaam,
Email = model.Email,
Voornaam = model.Voornaam,
Achternaam = model.Achternaam
};
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);
}
あなたの質問には十分ではありませんが、私が推測しなければならないのであれば、Entity Frameworkコンテキストで 'DropCreateDatabaseAlways'初期化子を採用していると思います。これで、アプリケーションを起動するたびに、データベースが消去され、初期状態に復元されます。 –
@ChrisPratt、DropCreateDatabaseAlwaysを使用せず、ソリューション全体のどこにでも初期化しました。十分な情報を得るためには何が必要ですか? – NathanBaele