爲認證服務端增長數據庫支持html
我計劃使用一個名爲Admin的表,放在一個已有的數據庫裏。因此我須要定義Admin類和在配置裏預先加上數據庫鏈接數據庫
新增類:Admin.csjson
public class Admin { public int Id { get; set; } public DateTime CreateDate { get; set; } public string UserName { get; set; } public string Password { get; set; } public string Remark { get; set; } }
配置上加上將要使用AdminContext的鏈接串,打開appsettings.json,添加紅圈部分。能夠按本身的數據庫鏈接修改鏈接串。也能夠不作這一部分,後面的過程IDE也會本身加上bash
在IdentityMvc項目的Controlers目錄右鍵,選擇「添加」=》「新搭建基架的項目」app
選擇「其操做使用Entity...的API控制器」框架
按「+」,添加數據上下文,修改圖上的,添加後,修改控制器的名稱爲「AccountControler"asp.net
「添加」後,框架會修改startup.cs,添加幾個文件。你能夠本身看看修改的內容,弄明白Entity的一些機制。若是數據庫沒有這個表,咱們能夠進行如下操做,讓框架幫咱們去建立async
點擊菜單:工具=》NuGet包管理器=》程序包管理器控制檯ide
Add-Migration CreateIdentitySchema Update-Database
Add-Migration
命令的 "CreateIdentitySchema" 名稱參數是任意的函數
執行完上面的兩個命令後,數據庫訪問就準備好了。如今要去修改AccountController,如今框架爲咱們生成的AccountCountroller並非咱們須要的,咱們要整個替換掉
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using IdentityMvc.Data; using IdentityServer4.Services; using IdentityServer4.Stores; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace IdentityMvc.Controllers { public class AccountController : Controller { private readonly AdminContext _dbAdmin; private readonly IIdentityServerInteractionService _interaction; private readonly IClientStore _clientStore; private readonly IAuthenticationSchemeProvider _schemeProvider; private readonly IEventService _events; public AccountController(IIdentityServerInteractionService interaction, IClientStore clientStore, IAuthenticationSchemeProvider schemeProvider, IEventService events, AdminContext db) { _interaction = interaction; _clientStore = clientStore; _schemeProvider = schemeProvider; _events = events; _dbAdmin = db; } /// <summary> /// 登陸頁面 /// </summary> [HttpGet] public async Task<IActionResult> Login(string returnUrl = null) { ViewData["returnUrl"] = returnUrl; return View(); } /// <summary> /// 登陸post回發處理 /// </summary> [HttpPost] public async Task<IActionResult> Login(string userName, string password, string returnUrl = null) { ViewData["returnUrl"] = returnUrl; Admin user = await GetByStr(userName, password); if (user != null) { AuthenticationProperties props = new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.Add(TimeSpan.FromDays(1)) }; await HttpContext.SignInAsync(user.Id.ToString(), user.UserName, props); if (returnUrl != null) { return Redirect(returnUrl); } return View(); } else { return View(); } } /// <summary> /// 驗證用戶,成功則返回用戶信息,不然返回null /// </summary> /// <param name="username"></param> /// <param name="pwd"></param> /// <returns></returns> public async Task<Admin> GetByStr(string username, string pwd) { Admin m = await _dbAdmin.Admin.Where(a => a.UserName == username && a.Password == pwd).SingleOrDefaultAsync(); if (m != null) { return m; } else { return null; } } } }
若是你對於爲何Controller的構造函數放這麼多參數,也但願看看哪裏構造這個AccountController.實際上是asp.net core的DI處理的,能夠看看這文章
https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html
好了,準備一下咱們的Login視圖就可測試了
在View目錄下新增」Account"目錄,右鍵Account目錄,「添加」=》「視圖」,錄入Login名稱,IDE將新建Login.cshtml,將其內容替換爲以下代碼:
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Login</title> </head> <body> <div align="center"> <h1>統一認證登陸中心</h1> <form action="/Account/Login" method="post"> 用戶名:<input type="text" name="userName" /><br /> 密 碼:<input type="password" name="password" /><input type="hidden" name="returnUrl" value="@ViewData["returnUrl"]" /> <br /> <input type="submit" value="登陸" /> </form> </div> </body> </html>
若是你都沒有忽略什麼的話,你應能在登陸後看到界面回到首頁下
但,如何你看到的是一個空白的頁面,地址相似於https://localhost:44302/signin-oidc的頁面,就說明,客戶端ClientMvc的startup.cs文件的Configure方法少了一句: