在ASP.NET Core Identity外面使用Cookie中间件
2017-01-13 14:40
786 查看
1、在 app.UseMvc 前面加上app.UseCookieAuthentication
2、登录
3、退出登录
4、判断是否已经登录
5、使用IIdentity拓展方法来获取存的值
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();
相关文章推荐
- 详解在ASP.NET Core 中使用Cookie中间件
- ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(三)—用户账户及cookie配置
- 在ASP.NET Core 中使用Cookie中间件
- IdentityServer(12)- 使用 ASP.NET Core Identity
- ASP.NET Core 2.0使用Cookie认证实现SSO单点登录
- asp.net core 使用 StaticFiles 中间件 (不完整翻译)
- ASP.NET Core 使用Cookie验证身份
- ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(四)—用户注册
- asp.net core 2.0 Cookie 使用
- ASP.NET Core 开发-中间件(StaticFiles)使用
- ASP.NET Core 使用Cookie验证身份
- ASP.NET Core的身份认证框架IdentityServer4(7)- 使用客户端证书控制API访问
- ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(一)—修改数据库连接
- ASP.NET Core 2.0使用Cookie认证实现SSO单点登录
- ASP.NET Core 使用 URL Rewrite 中间件实现 HTTP 重定向到 HTTPS
- ASP.NET Core 使用Cookie验证身份
- ASP.NET Core 中间件Diagnostics使用
- Asp.Net Core 简单的使用加密的Cookie保存用户状态
- 在ASP.NET Core 2.0中使用CookieAuthentication
- ASP.NET Core 使用Cookie验证身份