mvc core2.1 IdentityServer.EntityFramework Core 登录 (三)

Controllers->AccountController.cs 新建

 1 [HttpGet]
 2         [AllowAnonymous]
 3         public async Task<IActionResult> Login(string returnUrl = null)
 4         {
 5             // Clear the existing external cookie to ensure a clean login process
 6             await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
 8             ViewData["ReturnUrl"] = returnUrl;
 9             return View();
10         }
11
14         [HttpPost]
15         [AllowAnonymous]
16         [ValidateAntiForgeryToken]
17         public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
18         {
19             ViewData["ReturnUrl"] = returnUrl;
20             if (ModelState.IsValid)
21             {
22                 // This doesn't count login failures towards account lockout
23                 // To enable password failures to trigger account lockout, set lockoutOnFailure: true
24                 var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
25                 if (result.Succeeded)
26                 {
27                     return RedirectToLocal(returnUrl);
28                 }
37 else 38 { 39 ModelState.AddModelError(string.Empty, "Invalid login attempt."); 40 return View(model); 41 } 42 } 43 // If we got this far, something failed, redisplay form 44 return View(model); 45 } 46 47 [HttpPost] 48 [ValidateAntiForgeryToken] 49 public async Task<IActionResult> Logout() 50 { 51 await _signInManager.SignOutAsync(); 52 return RedirectToAction(nameof(HomeController.Index), "Home"); 53 }

Models->AccountViewModels->LoginViewModel.cs 新建

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace IdentityMvc.Models.AccountViewModels
{
    public class LoginViewModel
    {
        [Required]
        [EmailAddress]
        public string Email { get; set; }

        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }
}

Views->Account->Login.cshtml 新建

 1 @using System.Collections.Generic
 2 @using System.Linq
 3 @using Microsoft.AspNetCore.Http
 4 @using Microsoft.AspNetCore.Http.Authentication
 5 @model LoginViewModel
 6 @inject SignInManager<ApplicationUser> SignInManager
 7 
 8 @{
 9     ViewData["Title"] = "Log in";
10 }
11 
12 <h2>@ViewData["Title"]</h2>
13 <div class="row">
14     <div class="col-md-4">
15         <section>
16             <form asp-route-returnurl="@ViewData["ReturnUrl"]" method="post">
17                 <h4>Use a local account to log in.</h4>
18                 <hr />
19                 <div asp-validation-summary="All" class="text-danger"></div>
20                 <div class="form-group">
21                     <label asp-for="Email"></label>
22                     <input asp-for="Email" class="form-control" />
23                     <span asp-validation-for="Email" class="text-danger"></span>
24                 </div>
25                 <div class="form-group">
26                     <label asp-for="Password"></label>
27                     <input asp-for="Password" class="form-control" />
28                     <span asp-validation-for="Password" class="text-danger"></span>
29                 </div>
30                 <div class="form-group">
31                     <div class="checkbox">
32                         <label asp-for="RememberMe">
33                             <input asp-for="RememberMe" />
34                             @Html.DisplayNameFor(m => m.RememberMe)
35                         </label>
36                     </div>
37                 </div>
38                 <div class="form-group">
39                     <button type="submit" class="btn btn-default">Log in</button>
40                 </div>
41                 <div class="form-group">
42                     <p>
43                         <a asp-action="ForgotPassword">Forgot your password?</a>
44                     </p>
45                     <p>
46                         <a asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]">Register as a new user?</a>
47                     </p>
48                 </div>
49             </form>
50         </section>
51     </div>
52     <div class="col-md-6 col-md-offset-2">
53         <section>
54             <h4>Use another service to log in.</h4>
55             <hr />
56             @{
57                 var loginProviders = (await SignInManager.GetExternalAuthenticationSchemesAsync()).ToList();
58                 if (loginProviders.Count == 0)
59                 {
60                     <div>
61                         <p>
62                             There are no external authentication services configured. See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a>
63                             for details on setting up this ASP.NET application to support logging in via external services.
64                         </p>
65                     </div>
66                 }
67                 else
68                 {
69                     <form asp-action="ExternalLogin" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">
70                         <div>
71                             <p>
72                                 @foreach (var provider in loginProviders)
73                                 {
74                                     <button type="submit" class="btn btn-default" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.Name</button>
75                                 }
76                             </p>
77                         </div>
78                     </form>
79                 }
80             }
81         </section>
82     </div>
83 </div>
84 
85 @section Scripts {
86     @await Html.PartialAsync("_ValidationScriptsPartial")
87 }
View Code

退出登录只有一个方法

post  /Account/logout 

猜你喜欢

转载自www.cnblogs.com/LiuFengH/p/9418168.html