asp.net core 2.0 Cookie 使用
2017-08-19 09:35
671 查看
直接上代码
1、配置startup.cs 中
public void ConfigureServices(IServiceCollection services) { // 添加 Cook 服务 services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.LoginPath = "/Account/LogIn"; options.LogoutPath = "/Account/LogOff"; }); services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ...... // 使用Cook的中间件 app.UseAuthentication(); ...... }
2、登录
public IActionResult Login(){ var user = new ClaimsPrincipal( new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, username), }, CookieAuthenticationDefaults.AuthenticationScheme)); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user, new Microsoft.AspNetCore.Authentication.AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.Now.Add(TimeSpan.FromDays(7)) // 有效时间 }); return view(); }
3、退出登录
public async Task<IActionResult> Logout() { await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return RedirectToAction("Login", "Account"); }
相关文章推荐
- ASP.NET Core 2.0使用Cookie认证实现SSO单点登录
- ASP.NET Core 2.0使用Cookie认证实现SSO单点登录
- 在ASP.NET Core 2.0中使用CookieAuthentication
- 在ASP.NET CORE 2.0使用SignalR技术
- 在ASP.NET Core 2.0中使用MemoryCache
- ASP.NET Core 使用Cookie验证身份
- ASP.NET Core 2.0 使用支付宝PC网站支付
- .Net Core 2.0生态(3):ASP.NET Core 2.0 特性介绍和使用指南
- ASP.NET Core 使用Cookie验证身份
- ASP.NET Core 2.0 使用支付宝PC网站支付
- Asp.Net Core 简单的使用加密的Cookie保存用户状态
- ASP.NET Core 2.0 使用支付宝PC网站支付
- ASP.NET Core 2.0 使用支付宝PC网站支付实现代码
- ASP.NET Core 2.0 Cookie Authentication
- 在ASP.NET Core 2.0中使用CookieAuthentication
- ASP.NET Core 使用Cookie验证身份
- 在ASP.NET Core Identity外面使用Cookie中间件
- ASP.NET Core 2.0 特性介绍和使用指南
- ASP.NET Core 2.0 多应用实现Cookie共享
- ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(三)—用户账户及cookie配置