类库组件

.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. ELK(Elasticsearch6.0以上版本head插件安装)

    参考:https://www.cnblogs.com/Onlywjy/p/Elasticsearch.html Elasticsearch6.0不能使用命令直接安装head插件 修改配置文件/etc/ ...

  2. CCPC-Wannafly Winter Camp Day3 Div1 - 石头剪刀布 - [带权并查集]

    题目链接:https://zhixincode.com/contest/14/problem/I?problem_id=211 样例输入 1  3 5 2 1 1 2 1 2 1 1 2 3 2 1 ...

  3. [No0000DB]C# FtpClientHelper Ftp客户端上传下载重命名 类封装

    using System; using System.Diagnostics; using System.IO; using System.Text; using Shared; namespace ...

  4. Laravel 5.2 INSTALL- node's npm and ruby's bundler.

    https://getcomposer.org/doc/00-intro.md Introduction# Composer is a tool for dependency management i ...

  5. dyld环境变量

    苹果APP启动,分为两个过程:系统dylib动态链接库 app的main函数启动过程. main函数过程直接对iOS开发者.这里备忘的dylib过程: 一.dyld加载到虚拟内存     1. loa ...

  6. [ovs] openvswitch 入门

    https://www.sdnlab.com/sdn-guide/14747.html http://sdnhub.cn/index.php/openv-switch-full-guide/ http ...

  7. Flink - FlinkKafkaConsumer010

    Properties properties = new Properties(); properties.setProperty("bootstrap.servers", &quo ...

  8. python,re模块正则

    python没有正则需要导入re模块调用.正则表达式是为了匹配字符串,动态模糊的匹配,只要有返回就匹配到了, 没返回就没匹配到,前面是格式后面是字符串 最常用的匹配语法: re.match()#麦驰, ...

  9. 20165317JAVA实验二-面向对象程序设计

    JAVA实验二-面向对象程序设计 提交点一 参考Intellj IDEA 简易教程-单元测试完成单元测试的学习 在IDEA中建立名为MyUtil5317的project,并在其src文件夹中创建名为M ...

  10. LeetCode 463 Island Perimeter 解题报告

    题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...