在ASP.NET Core Identity外面使用Cookie中间件
来源:互联网 发布:推荐软件推荐 知乎 编辑:程序博客网 时间:2024/06/05 03:08
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(20),
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();
0 0
- 在ASP.NET Core Identity外面使用Cookie中间件
- ASP.NET Core 中间件Diagnostics使用
- ASP.NET Core 中间件
- Asp.Net Core-Identity 配置
- Asp.Net Core-Identity迁移
- asp.net core 2.0 Cookie 使用
- ASP.NET Core 使用Cookie验证身份
- ASP.NET Core 使用Cookie验证身份
- ASP.NET Core 使用Cookie验证身份
- ASP.NET CORE基础教程(二)-中间件
- ASP.NET Core 中间件之压缩、缓存
- ASP.NET Core 开发-中间件(Middleware)
- [译]ASP.NET Core 2.0 中间件
- ASP.NET Identity 使用简介
- ASP.NET WebForm Identity使用
- Cookie在ASP.NET编程的使用
- net core使用加密cookie
- ASP.NET Core 之 Identity 入门(一)
- UIImage的2种加载方式
- Java SE学习笔记:Java Bean编写规范,System类,Math类
- hadoop标准序列化过程
- ftp和sftp两个命令
- 简单的图像分类练习
- 在ASP.NET Core Identity外面使用Cookie中间件
- 专为挑剔者:电子商务网站用户界面设计模板
- Meta标签中的format-detection属性及含义
- div等元素height:100%高度为什么不生效
- HTTP访问控制
- C# Socket TCP通信,连接服务器
- Android实用工具类——ListView动态显示条目数目
- .net core日志 NLog.Extensions.Logging
- The method getTextContent() is undefined for the type Node