Как исправить Ваш логин для загрузки при использовании ASP.NET MVC?
Привет Команда
Я создал логин, но он не загружается. routeConfig действительно сопоставляет действие с контроллером. У самого контроллера есть имя метода, а также представление.
Что я уже пробовал:
// Класс контроллера
// GET: /Account/Login [AllowAnonymous] public ActionResult Login(string returnUrl) { TempData["ErrorMessage"] = ""; TempData["LoginMessage"] = ""; ViewBag.ReturnUrl = returnUrl; return View(); } // // POST: /Account/Login [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Index(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return View(model); } var emailExist = await UserManager.FindByEmailAsync(model.Email); if(emailExist != null) { if(emailExist.EmailConfirmed == false) { string code = await UserManager.GenerateEmailConfirmationTokenAsync(emailExist.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = emailExist.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync(emailExist.Id, "Confirm your account", ConfirmAccountMailBody(callbackUrl)); TempData["ErrorMessage"] = "Email id is not verified. Please check your email and verify account !!!"; ViewBag.JavaScriptFunction = "ShowErrorPopup();"; return View(model); } } else { TempData["ErrorMessage"] = "Email is not registered !!!"; ViewBag.JavaScriptFunction = "ShowErrorPopup();"; return View(model); } var loggedinUser = await UserManager.FindAsync(model.Email, model.Password); if(loggedinUser !=null) { await UserManager.UpdateSecurityStampAsync(loggedinUser.Id); } // 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: TempData["ErrorMessage"] = "Email or Password is Incorrect"; ViewBag.JavaScriptFunction = "ShowErrorPopup();"; return View(model); } }
// Route-Config
// Route to Login. routes.MapRoute( name: "Login", url: "login/", defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional } );
/ Индекс/.cshtml по
@using eNtsaRegistrationTraining.Models @model LoginViewModel @{ ViewBag.Title = "Login"; Layout = "~/Views/Shared/_LoginLayout.cshtml"; } <div class="login-box"> <div class="login-logo"> <a href="@Url.RouteUrl("Login")">eNtsa Registration</a> </div> <!-- /.login-logo --> <div class="card"> <div class="card-body login-card-body"> <p class="login-box-msg">Sign in to start your session</p> @using (Html.BeginForm("Index", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @role = "form" })) { @Html.AntiForgeryToken() <div class="input-group mb-3"> @Html.TextBoxFor(m => m.Email, new { @class = "form-control", type = "email", placeholder = "Email", autofocus = "autofocus", required = "required" }) <div class="input-group-append"> <div class="input-group-text"> <span class="fas fa-envelope"></span> </div> </div> </div> <div class="input-group mb-3"> @Html.PasswordFor(m => m.Password, new { @class = "form-control", type = "password", placeholder = "Password" }) <div class="input-group-append"> <div class="input-group-text"> <span class="fas fa-lock"></span> </div> </div> </div> <div class="row"> <div class="col-8"> <div class="icheck-primary"> @Html.CheckBoxFor(m => m.RememberMe, new { @type = "checkbox", id = "remember" }) <label for="remember"> Remember Me </label> </div> </div> <!-- /.col --> <div class="col-4"> <button type="submit" class="btn btn-primary btn-block">Sign In</button> </div> <!-- /.col --> </div> } <div class="social-auth-links text-center mb-3"> <section id="socialLoginForm"> @*@Html.Partial("_ExternalLoginsListPartial", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })*@ </section> </div> <!-- /.social-auth-links --> <p class="mb-1"> <a href="@Url.RouteUrl("ForgotPasswd")">Forgot your password?</a> </p> <p class="mb-0"> <a href="@Url.RouteUrl("Register")" class="text-center">Register a new membership</a> </p> </div> <!-- /.login-card-body --> </div> </div> <div class="modal fade" id="modal-danger" tabindex="-1" role="dialog" data-keyboard="false" data-backdrop="static"> <div class="modal-dialog"> <div class="modal-content bg-danger"> <div class="modal-header"> <h4 class="modal-title">Authentication Failed !</h4> </div> <div class="modal-body"> @TempData["ErrorMessage"] </div> <div class="modal-footer justify-content-between"> <button type="button" class="btn btn-outline-light" data-dismiss="modal">OK</button> </div> </div> </div> </div> <script type="text/javascript"> function ShowErrorPopup() { $("#modal-danger").modal(); } </script> @if (ViewBag.JavaScriptFunction != null) { <script type="text/javascript"> @Html.Raw(ViewBag.JavaScriptFunction) </script> }
[no name]
Привет, пожалуйста, измените название метода действия "индекс" на "логин"
// Маршрут для входа в систему.
маршруты.MapRoute(
Имя: "Логин",
url: "вход в систему/",
по умолчанию: новый { контроллера счет= "", действие = "Логин" идентификатор = UrlParameter.Необязательный }
);
gcogco10
Спасибо