.net core 学习小结之 Cookie-based认证
- 在startup中添加授权相关的管道
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; namespace mvcforcookie
{
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication.Cookies;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(option => option.LoginPath = "/Acounnt/Index");
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
} - 将需要权限访问的页面贴上特性标签
[Authorize(Roles="Admin")] 表名只有Admin身份的人才能进入Admin控制器
- 用户成功输入用户名和密码之后生成用户票据
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using mvcforcookie.Models; namespace mvcforcookie.Controllers
{
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using System.Security.Claims;
public class AcounntController : Controller
{
public IActionResult Index()
{
//数据库查询用户输入的用户名和密码等一系列匹配操作
//模拟用户登录后的操作
//创建一个用户身份
var claims=new List<Claim>{
new Claim(ClaimTypes.Name,"cyao"),
new Claim(ClaimTypes.Role,"Admin")
};
var claimidentity=new ClaimsIdentity(claims,CookieAuthenticationDefaults.AuthenticationScheme);
//向上下文容器中添加当前用户
HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,new ClaimsPrincipal(claimidentity));
return Ok();
}
public IActionResult LoginOut()
{
HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return Ok();
}
}
} - 如果要获取当前用户的身份和用户名的话
ViewBag.User= User.Claims.Where(c =>c.Type==ClaimTypes.Name).First().Value;
ViewBag.Type= User.Claims.Where(c =>c.Type==ClaimTypes.Role).First().Value;
.net core 学习小结之 Cookie-based认证的更多相关文章
- .net core 学习小结之 JWT 认证授权
新增配置文件 { "Logging": { "IncludeScopes": false, "Debug": { "LogLeve ...
- .net core 学习小结之环境配置篇
安装IIs对 netcore 的支持 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/aspnet-core-mod ...
- .net core 学习小结之 自定义JWT授权
自定义token的验证类 using System; using System.Collections.Generic; using System.IO; using System.Linq; usi ...
- .net core 学习小结之 PostMan报415
首先415的官方解释是:对于当前请求的方法和所请求的资源,请求中提交的实体并不是服务器中所支持的格式,因此请求被拒绝. 也就是说我所准备的数据格式并不是后台代码使用的数据格式 后台代码如下 using ...
- .net core 学习小结之 配置介绍(config)以及热更新
命令行的配置 var settings = new Dictionary<string, string>{ { "name","cyao"}, {& ...
- ASP.NET CORE中使用Cookie身份认证
大家在使用ASP.NET的时候一定都用过FormsAuthentication做登录用户的身份认证,FormsAuthentication的核心就是Cookie,ASP.NET会将用户名存储在Cook ...
- NET Core 2.0使用Cookie认证实现SSO单点登录
NET Core 2.0使用Cookie认证实现SSO单点登录 之前写了一个使用ASP.NET MVC实现SSO登录的Demo,https://github.com/bidianqing/SSO.Sa ...
- Asp.net core 学习笔记 ( identity server 4 JWT Part )
更新 : id4 使用这个 DbContext 哦 dotnet ef migrations add identity-server-init --context PersistedGrantDbCo ...
- .NET Core 学习资料精选:入门
开源跨平台的.NET Core,还没上车的赶紧的,来不及解释了-- 本系列文章,主要分享一些.NET Core比较优秀的社区资料和微软官方资料.我进行了知识点归类,让大家可以更清晰的学习.NET Co ...
随机推荐
- Prism MVVM使用WPF的DataGrid控件
此项目源码下载地址:https://github.com/lizhiqiang0204/PrismDataGird01 运行效果如下 前端代码如下 <Window x:Class="V ...
- 详解GSM的基带跳频和射频跳频
跳频技术源于军事通信,目的是为了获得较好的保密性和抗干扰能力.跳频分为快速和慢速两种,GSM中的跳频属于慢跳频. 跳频方式从时域概念上分为帧跳频和时隙跳频,从载频实现方式上分为射频跳频和基带跳频. 帧 ...
- Mysql 连接路径 url 参数解析
1.mysql - url 参数解析 url:jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf8 u ...
- CDOJ 1069 秋实大哥去打工 单调栈 下标处理
E - 秋实大哥去打工 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit St ...
- codevs 1126 数字统计 2010年NOIP全国联赛普及组 x
题目描述 Description 请统计某个给定范围[L, R]的所有整数中,数字2出现的次数. 比如给定范围[2, 22],数字2在数2中出现了1次,在数12中出现1次,在数20中出现1次,在数21 ...
- Tree and Permutation
Tree and Permutation 给出一个1,2,3...N的排列,显然全部共有N!种排列,每种排列的数字代表树上的一个结点,设Pi是其中第i种排列的相邻数字表示的结点的距离之和,让我们求su ...
- Selenium-webdriver+八种元素定位
进行Web页面自动化测试,对页面上的元素进行定位和操作是核心.而操作又是以定位为前提的,因此,对页面元素的定位是进行自动化测试的基础. 页面上的元素就像人一样,有各种属性,比如元素名字,元素id,元素 ...
- 关于项目中的一些经验:封装activity、service的基类,封装数据对象
经验一,将几个页面公用的数据,和方法进行封装,形成一个baseActivity的类: package com.ctbri.weather.control; import java.util.Array ...
- 9.并发编程--ThreadLocal
并发编程--ThreadLocal 1. ThreadLocal : * 线程局部变量,是一种多个线程间并发访问变量的解决方案. * 与其使用synchronized等加锁的方式,ThreadLoca ...
- 火狐使用阿里云OOS上传图片报错:“XML 解析错误:找不到根元素”
问题描述: 使用阿里云OOS上传图片在火狐浏览器报错 "XML 解析错误:找不到根元素",但不影响功能的使用.阿里云返回信息: <Error> <Code> ...