如何解决如何将视图重定向到主页-ASP.NET Core MVC
这是我的第一个ASP.NET Core MVC项目,也是第一次C#经验。我正在创建一个模仿ubereats / doordash的网络应用。
现在,我正在对注册/登录系统进行编码,并将其与sql数据库合并。注册/创建帐户功能运行良好,但是现在我需要弄清楚如何在某人登录时对他们进行身份验证。
所以,我编写算法的方式是:
- 用户创建帐户-数据库已更新(效果很好)。
- 用户单击登录超链接-将他们带到登录页面(同样有效)
- 用户输入电子邮件和密码,然后按提交(我在这里没有收到任何错误,但我认为我没有正确创建/验证身份)。
- 控制器获取用户输入的电子邮件和密码-如果数据库找到了电子邮件,则检查密码是否正确。
- 如果正确-将用户重定向到首页,否则刷新并再次显示登录表单。
我需要帮助来遍历和检索数据库中的项目,然后验证它们是否正确。
如果登录成功,我还需要帮助将用户重定向到主页(位于视图中“主页”文件夹内)。 C#和asp.net似乎非常先进,所以我想知道是否有一个我不知道的简单函数可能有用。
有人可以帮助我使登录部分正常工作吗?
我认为登录控制器的这一特定部分是问题:
// GET: Logins/Create
public IActionResult LoginPortal()
{
return View();
}
[HttpPost]
public async Task<IActionResult> LoginPortal(int id)
{
var movie = await _context.Login.FindAsync(id);
if (movie == null)
{
return View();
}
return View("Home/Index");
}
这是我到目前为止所拥有的:
LoginsController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; //including necessary directories
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using EasyEats.Data;
using EasyEats.Models;
namespace EasyEats.Controllers
{
public class LoginsController : Controller //log-in & register controller
{
private readonly EasyEatsContext _context;
public LoginsController(EasyEatsContext context)
{
_context = context;
}
// GET: Logins
public async Task<IActionResult> Index()
{
return View(await _context.Login.ToListAsync());
}
// GET: Logins/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var login = await _context.Login
.FirstOrDefaultAsync(m => m.ID == id);
if (login == null)
{
return NotFound();
}
return View(login);
}
// GET: Logins/Create
public IActionResult Create()
{
return View();
}
// POST: Logins/Create
// To protect from overposting attacks,enable the specific properties you want to bind to,for
// more details,see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,FirstName,LastName,Address,Email,PhoneNumber,Password")] Login login)
{
if (ModelState.IsValid)
{
_context.Add(login);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(login);
}
// GET: Logins/Create
public IActionResult LoginPortal()
{
return View();
}
[HttpPost]
public async Task<IActionResult> LoginPortal(int id)
{
var movie = await _context.Login.FindAsync(id);
if (movie == null)
{
return View();
}
return View("Home/Index");
}
// POST: Logins/Edit/5
// To protect from overposting attacks,see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id,[Bind("ID,Password")] Login login)
{
if (id != login.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(login);
await _context.SaveChangesAsync();
}
catch (dbupdateConcurrencyException)
{
if (!LoginExists(login.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(login);
}
// GET: Logins/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var login = await _context.Login
.FirstOrDefaultAsync(m => m.ID == id);
if (login == null)
{
return NotFound();
}
return View(login);
}
// POST: Logins/Delete/5
[HttpPost,ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var login = await _context.Login.FindAsync(id);
_context.Login.Remove(login);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool LoginExists(int id)
{
return _context.Login.Any(e => e.ID == id);
}
}
}
Login.cs (型号)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EasyEats.Models
{
public class Login //class for customer log-in & registration information
{
public int ID { get; set; }
[required]
[display(Name = "First Name")]
public string FirstName { get; set; }
[required]
[display(Name = "Last Name")]
public string LastName { get; set; }
[required]
public string Address { get; set; }
[required]
public string Email { get; set; }
[required]
[display(Name = "Phone Number")]
public int PhoneNumber { get; set; }
[required]
public string Password { get; set; }
}
}
在“视图”文件夹中:我有一个主文件夹,“登录名”文件夹和“共享”文件夹
Index.cshtml (位于“视图”的“主页”文件夹中)
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
LoginPortal.cshtml (在“视图”中的“登录”文件夹内部)
@model EasyEats.Models.Login
@{
ViewData["Title"] = "Create";
}
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="LoginPortal">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password" class="control-label"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Log in" class="btn btn-primary" />
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
_Layout.cshtml (在共享文件夹中)
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="utf-8" />
<Meta name="viewport" content="width=device-width,initial-scale=1.0" />
<title>@ViewData["Title"] - EasyEats</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom Box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">EasyEats</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<partial name="_LoginPartial" />
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
© 2020 - EasyEats - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@RenderSection("Scripts",required: false)
</body>
</html>
_LoginPartial.cshtml (在共享文件夹中)
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Logins"
asp-action="Create">Register</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Logins"
asp-action="LoginPortal">Login</a>
</li>
</ul>
Create Account page Login Page
解决方法
我需要帮助来遍历和检索数据库中的项目,并且 然后验证它们是否正确。
您可以在数据库中找到模型,该模型的电子邮件与从表单中发布的电子邮件相同。然后检查找到的模型的密码。
如果登录成功,我还需要帮助将用户重定向到主页(位于视图中“主页”文件夹内)。
您可以使用return RedirectToAction("Index","Home");
。
这是一个工作示例:
控制器:
public IActionResult LoginPortal()
{
return View();
}
[HttpPost]
public async Task<IActionResult> LoginPortal(Login login)
{
//for easy testing,i just hardcode the list
var list = new List<Login>() {
new Login(){Email="a",Password="123"},new Login(){Email="b",Password="111"}
};
//you could find it in the database
//var movie = _context.Login.Where(a => a.Email == login.Email).FirstOrDefault();
var movie = list.Where(a => a.Email == login.Email).FirstOrDefault();
if (movie == null)
{
return View(login);
}
if (movie.Password==login.Password)
{
return RedirectToAction("Index","Home");
}
return View();
}
更新:
您可以按如下所示设置ModelState错误:
[HttpPost]
public async Task<IActionResult> LoginPortal(Login login)
{
//...
if (movie == null)
{
ModelState.AddModelError("Email","Do not find such email");
return View(login);
}
if (movie.Password==login.Password)
{
return RedirectToAction("Index","Home");
}
ModelState.AddModelError(string.Empty,"invalid login attempt");
return View();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。