【发布时间】:2021-02-24 00:52:56
【问题描述】:
这是我的第一个 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, 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> Edit(int id, [Bind("ID,FirstName,LastName,Address,Email,PhoneNumber,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>
以下是一些图片: Homepage
【问题讨论】:
标签: c# html asp.net-mvc asp.net-core model-view-controller