Commit e47e27f3 authored by 賴勝傑's avatar 賴勝傑

添加:

    - 前端
        添加 Login 頁面。
        添加 Home Logout 按鈕。
    - 後端
        添加 Login  功能,未使用 Model 接參數,認證授權。
        添加 Logout 功能。
        添加 HomeController [Authorize]。
parent 3cd30b5c
......@@ -5,6 +5,8 @@ VisualStudioVersion = 17.12.35527.113 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrainingDemo", "TrainingDemo\TrainingDemo.csproj", "{53D8DE69-8CCD-4E05-890D-D9D8788A9C99}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrainingDemo.Model", "TrainingDemo.Model\TrainingDemo.Model.csproj", "{EE31966E-DFB7-473D-B20F-2D3B9C662EEF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -15,6 +17,10 @@ Global
{53D8DE69-8CCD-4E05-890D-D9D8788A9C99}.Debug|Any CPU.Build.0 = Debug|Any CPU
{53D8DE69-8CCD-4E05-890D-D9D8788A9C99}.Release|Any CPU.ActiveCfg = Release|Any CPU
{53D8DE69-8CCD-4E05-890D-D9D8788A9C99}.Release|Any CPU.Build.0 = Release|Any CPU
{EE31966E-DFB7-473D-B20F-2D3B9C662EEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE31966E-DFB7-473D-B20F-2D3B9C662EEF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE31966E-DFB7-473D-B20F-2D3B9C662EEF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE31966E-DFB7-473D-B20F-2D3B9C662EEF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity.Data;
using Microsoft.AspNetCore.Mvc;
namespace TrainingDemo.Controllers
......@@ -6,6 +10,36 @@ namespace TrainingDemo.Controllers
public class AccountController : Controller
{
[HttpPost]
public IActionResult Login(string Account, string Password)
{
string account = "abc";
string passoword = "123";
if (!account.Equals(Account) || !passoword.Equals(Password))
return View("Login");
List<Claim> claims = new()
{
new Claim(ClaimTypes.Name,Account)
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity));
return RedirectToAction("Index", "Home");
}
[HttpGet]
public IActionResult Logout()
{
HttpContext.SignOutAsync("Cookies");
return View("Login");
}
[HttpGet]
[AllowAnonymous]
public IActionResult Login()
{
......
......@@ -15,21 +15,19 @@
</head>
<body class="container-fluid vh-100 d-flex justify-content-center align-items-center">
<div style="width: 400px;">
<form id="loginForm" class="form">
<form id="loginForm" class="form" asp-controller="Account" asp-action="Login" method="post">
<div class="mb-3">
<label for="account" class="form-label">Account</label>
<input type="text" id="account" name="account" class="form-control" placeholder="Input Account" />
<input type="text" id="account" name="Account" class="form-control" placeholder="Input Account" />
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" id="password" name="password" class="form-control" placeholder="Input Password" />
<input type="password" id="password" name="Password" class="form-control" placeholder="Input Password" />
</div>
<button type="submit" class="btn btn-primary w-100">Login</button>
</form>
</div>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
</body>
......
......@@ -27,6 +27,7 @@
</li>
</ul>
</div>
<a class="nav-link text-dark" asp-area="" asp-controller="Account" asp-action="Logout">Logout</a>
</div>
</nav>
</header>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment