.Net Core Identity外面使用Cookie中间件
1、在 app.UseMvc 前面加上app.UseCookieAuthentication
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "IdeaCoreUser",
LoginPath = new PathString("/Login/Login/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true,
CookieDomain=""
});
2、登录
var claims = new List<Claim> {
new Claim("FullName", customer.Username,ClaimValueTypes.String),
new Claim("Role", "注册用户",ClaimValueTypes.String),
};
var userIdentity = new ClaimsIdentity(claims, "Customer");
var userPrincipal = new ClaimsPrincipal(userIdentity);
HttpContext.Authentication.SignInAsync("IdeaCoreUser", userPrincipal,
new AuthenticationProperties
{
ExpiresUtc = DateTime.UtcNow.AddMinutes(),
IsPersistent = false,
AllowRefresh = false
});
3、退出登录
HttpContext.Authentication.SignOutAsync("IdeaCoreUser");
4、判断是否已经登录
var bol =HttpContext.User.Identity.IsAuthenticated;
5、使用IIdentity拓展方法来获取存的值
public static class IdentityExtension
{
public static string FullName(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("FullName");
return (claim != null) ? claim.Value : string.Empty;
}
public static string Role(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("Role");
return (claim != null) ? claim.Value : string.Empty;
}
}
var fullname = HttpContext.User.Identity.FullName();
.Net Core Identity外面使用Cookie中间件的更多相关文章
- .NET Core 从1.1升级到2.0记录(Cookie中间件踩坑)
.NET Core 2.0 新时代 万众瞩目的.NET Core 2.0终于发布了,原定于9.19的dotnetconf大会的发布时间大大提前了1个月,.NET Core 2.0/.NET Stand ...
- 在ASP.NET Core 中使用Cookie中间件
在ASP.NET Core 中使用Cookie中间件 ASP.NET Core 提供了Cookie中间件来序列化用户主题到一个加密的Cookie中并且在后来的请求中校验这个Cookie,再现用户并且分 ...
- 在ASP.NET Core 中使用Cookie中间件 (.net core 1.x适用)
在ASP.NET Core 中使用Cookie中间件 ASP.NET Core 提供了Cookie中间件来序列化用户主题到一个加密的Cookie中并且在后来的请求中校验这个Cookie,再现用户并且分 ...
- .NET Core 2.0 Cookie中间件 权限验证
:在ConfigureServices添加Cookie中间件,使用自定义Scheme services.AddAuthentication(options=> { options.Default ...
- ASP.NET Core 中的那些认证中间件及一些重要知识点
前言 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 A ...
- [转]ASP.NET Core 中的那些认证中间件及一些重要知识点
本文转自:http://www.qingruanit.net/c_all/article_6645.html 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系 ...
- ASP.NET Core Identity Hands On(2)——注册、登录、Claim
上一篇文章(ASP.NET Core Identity Hands On(1)--Identity 初次体验)中,我们初识了Identity,并且详细分析了AspNetUsers用户存储表,这篇我们将 ...
- ASP.NET Core Identity 实战(2)——注册、登录、Claim
上一篇文章(ASP.NET Core Identity Hands On(1)--Identity 初次体验)中,我们初识了Identity,并且详细分析了AspNetUsers用户存储表,这篇我们将 ...
- ASP.NET Core Identity 实战(3)认证过程
如果你没接触过旧版Asp.Net Mvc中的 Authorize 或者 Cookie登陆,那么你一定会疑惑 认证这个名词,这太正式了,这到底代表这什么? 获取资源之前得先过两道关卡Authentica ...
随机推荐
- ubuntu_虚拟机和SD卡链接失败,可能的原因
这个问题很简单吧,但是自己解决却用了很长时间,说一下方法吧! 1.有的虚拟机不兼容USB3.0的接口,所以在接SD卡(读卡器)时,请将读卡器拔出,插入笔记本USB2.0的接口上(当时自己没注意到这点, ...
- POJ 1118 Lining Up
枚举,排序. 先将所有点按双关键字排序,然后枚举线的顶点$P$,剩余的点以$P$为中心进行极角排序,可以取个$gcd$,这样一样的点就排在一起了,然后统计一下更新答案. #pragma comment ...
- angularJS懒加载依赖模块
//设置 .config [ '$ocLazyLoadProvider' ($ocLazyLoadProvider) -> # We configure ocLazyLoad to use th ...
- Python学习笔记——基础篇【第五周】——正在表达式(re.match与re.search的区别)
目录 1.正在表达式 2.正则表达式常用5种操作 3.正则表达式实例 4.re.match与re.search的区别 5.json 和 pickle 1.正则表达式 语法: import re # ...
- AngularJS 初学笔记(理论基础)
AngularJS 是一个 JavaScript 框架.它是一个以 JavaScript 编写的库. AngularJS 通过 ng-directives 扩展了 HTML. ng-app 指令定义一 ...
- Unix时间戳转换成C#中的DateTime
先交代一下应用场景:我们的软件需要做一个简单的有效期验证保护.初始的想法是 在本地将安装时间.启动时间.当前时间做比较,为了防止记录被修改,记录在注册表的特殊的地方并加密. 我使用了.net自带的rs ...
- LeetCode 395. Longest Substring with At Least K Repeating Characters C#
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- 如何在IIS8.5上面部署php
一.开启,设置win8.1自带的IIS 8.5组件服务器. 进入控制面板,选择程序和功能,打开或关闭Windows 功能,找到Internet information services,分别开启FTP ...
- python获取
def anc():pass print anc.__name__ def timeit(func): def run(*argv): print "this function name i ...
- java 图形界面 邮件系统
将后台的邮件系统使用javaGUI编程来实现,让我们可以在桌面端直接控制邮件的收发功能. 一.实现邮箱的登陆功能 邮件系统使用smtp协议发送邮件,使用POP3协议或者IMAP协议来收取邮件.SMTP ...