类库组件

.NET Core的身份认证使用的类库如下图:常用的

Microsoft.AspNetCore.Authorization

Microsoft.AspNetCore.Authorization.Cookies

Microsoft.AspNetCore.Authorization.OpenIdConnect

Microsoft.AspNetCore.Authorization.OAuth

演示下基于Cookies的

Startup.cs添加管道支持:

ConfigureService:

services.AddAuthorization(); 

Configure:

 app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookie",
LoginPath = new PathString("/Account/Login"),
AccessDeniedPath = new PathString("/Account/Forbidden"),
AutomaticAuthenticate = true,
AutomaticChallenge = true
});

环境支持配置完以后;老套路简单使用一下

Controller或者Action添加[Authorize];Claim声明一些属性,加入到ClaimIdentity(IIdentity)属性标识;通过ClaimIdentity再创建身份ClaimPrincipal(IPrincipal)出来;存入Cookie

AccountController :

public class AccountController : Controller
{
[Authorize]
// GET: /<controller>/
public IActionResult Index()
{
return View();
} [HttpGet]
public IActionResult Login()
{
return View();
} [HttpPost]
public async Task<IActionResult> Login(LoginViewModel model)
{
if (model.Username.Equals("admin")&&model.Password.Equals(""))
{
//名片
List<Claim> claims = new List<Claim>
{
new Claim(ClaimTypes.Name,model.Username)
};
//身份
ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(claims,"Login")); await HttpContext.Authentication.SignInAsync("Cookie", principal, new AuthenticationProperties {
ExpiresUtc = DateTime.UtcNow.AddMinutes(),
IsPersistent=false,
AllowRefresh=false,
}); return RedirectToAction("Index","Account"); }
else
{
return Content("用户名密码错误!");
} } public async Task<IActionResult> Logout()
{
await HttpContext.Authentication.SignOutAsync("Cookie"); return RedirectToAction("Index", "Home");
}
    public class LoginViewModel
{
[Required]
public string Username { get; set; }
[Required]
public string Password { get; set; } }
@model Practice.WebClient.Models.LoginViewModel
@{
ViewData["Title"] = "Login";
} <h2>登录</h2>
@using (Html.BeginForm("Login", "Account", new { returnUrl = ViewBag.ReturnUrl }, FormMethod.Post))
{
@Html.AntiForgeryToken() <!-- 登录框 -->
<div class="loginBox loginAndReg">
<h3>账号登入</h3>
<span style="color:red"> @Html.ValidationSummary(true, "")</span>
<p class="userName">
<span class="icon"><i></i></span>
<label>
@Html.TextBoxFor(m => m.Username, new { @placeholder = "请输入登录账号", @class = "changeInput" })
<em class="clean"></em>
</label> </p>
<p class="userPassword">
<span class="icon"><i></i></span>
<label>
@Html.PasswordFor(m => m.Password, new { @placeholder = "请输入登录密码", @class = "changeInput" })
<em class="clean"></em>
</label>
</p>
<button type="submit" class="loginBtn" id="inputLogin">登 录</button>
</div> }

Login.cshtml

@{
ViewData["Title"] = "账户中心";
} <h2>账户中心</h2> <h2>Claim:</h2>
<dl>
@foreach (var claim in User.Claims)
{
<dt>@claim.Type</dt>
<dd>@claim.Value</dd> }
</dl>

Index.cshtml

.Net Core:身份认证组件的更多相关文章

  1. .Net Core身份认证:IdentityServer4实现OAuth 2.0 客户端模式 - 简书

    原文:.Net Core身份认证:IdentityServer4实现OAuth 2.0 客户端模式 - 简书 一.客户端模式介绍 客户端模式(Client Credentials Grant)是指客户 ...

  2. 深入解读 ASP.NET Core 身份认证过程

    长话短说:上文我们讲了 ASP.NET Core 基于声明的访问控制到底是什么鬼? 今天我们乘胜追击:聊一聊ASP.NET Core 中的身份验证. 身份验证是确定用户身份的过程. 授权是确定用户是否 ...

  3. Core身份认证

    Core中实现一个基础的身份认证 注:本文提到的代码示例下载地址> How to achieve a basic authorization in ASP.NET Core 如何在ASP.NET ...

  4. .NET 黑魔法 - asp.net core 身份认证 - Policy

    身份认证几乎是每个项目都要集成的功能,在面向接口(Microservice)的系统中,我们需要有跨平台,多终端支持等特性的认证机制,基于token的认证方式无疑是最好的方案.今天我们就来介绍下在.Ne ...

  5. ASP.NET Core 身份认证 (Identity、Authentication)

    Authentication和Authorization 每每说到身份验证.认证的时候,总不免说提及一下这2个词.他们的看起来非常的相似,但实际上他们是不一样的. Authentication想要说明 ...

  6. asp.net core 身份认证/权限管理系统简介及简单案例

    如今的网站大多数都离不开账号注册及用户管理,而这些功能就是通常说的身份验证.这些常见功能微软都为我们做了封装,我们只要利用.net core提供的一些工具就可以很方便的搭建适用于大部分应用的权限管理系 ...

  7. .Net Core身份认证:IdentityServer4实现OAuth 2.0 客户端模式

    一.客户端模式介绍 客户端模式(Client Credentials Grant)是指客户端直接向认证服务(Authorization Server)发送认证请求,获取token,进行认证,一般适用于 ...

  8. Aspen.net core 身份认证

  9. .Net Core 授权系统组件解析

    前面关于.Net Core如何进行用户认证的核心流程介绍完毕之后,.Net Core 认证系统之Cookie认证源码解析远程认证暂时不介绍,后期有时间,我会加上.接下去介绍认证组件是如何和认证组件一起 ...

随机推荐

  1. 2018ACM-ICPC南京区域赛---AJGIDKM

    含[最小球覆盖][最大流isap]模板. 题面pdf https://codeforc.es/gym/101981/attachments/download/7891/20182019-acmicpc ...

  2. .NET Core开发日志——ADO.NET与SQL Server

    在.NET世界,如果想要对数据库进行操作,总少不了ADO.NET的身影.在.NET Core里同样离不开那些熟悉的类库与API.这里简略地介绍下如何通过ADO.NET对SQL Server进行不同的处 ...

  3. [No0000E7]C# 封装 与访问修饰符

    C# 支持的访问修饰符: Public Private Protected Internal Protected internal Public 访问修饰符 Public 访问修饰符允许一个类将其成员 ...

  4. Fiddler怎么可以抓取https的请求包

    对于https的协议是带有安全的功能,所有有一些的https的协议是无法抓到的,所以需要通过设置filler中来对,来使用filler的方式的来抓取到https的请求包,那么如何使用filler中抓取 ...

  5. UI自动化遇到的问题

    1.找不到元素:查看是否有[frame]或者[iframe]元素 2.click无响应:1)使用sendkeys(Keys.Enter) 还是无响应:使用js定位 3.弹框处理: 确认:switcht ...

  6. jquery有几种选择器?

    ①.基本选择器:#id,class,element,*: ②.层次选择器:parent > child,prev + next,prev ~ siblings: ③.基本过滤器选择器::firs ...

  7. oracle中not in 和 in 的替代写法

    -- not in 的替代写法select col from table1 where col not in(select col from table2); select col,table2.co ...

  8. oracle中,将两个select语句的结果作为一个整体显示出来

    如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并在一起显示出来.union和union ...

  9. Eclipse EE下载安装与配置

    Eclipse EE下载安装与配置 一.下载 下载链接:http://www.eclipse.org/downloads/eclipse-packages/ 1.进入Eclipse官网进行下载选择Ec ...

  10. vc让界面保持最上层

    vc让界面保持最上层.事实上就一个函数就ok了, ::SetWindowPos(AfxGetMainWnd()->m_hWnd,HWND_TOPMOST,-1,-1,-1,-1,0);