背景是在一个项目中增加临时登录功能,只需验证用户是否登录即可,所需的最低配置与实现代码如下。

在 Startup 的 ConfigureServices() 方法中添加 Authentication 的配置:

services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie();

在 Startup 的 Configure() 方法中将 Authentication 添加到请求管线:

app.UseAuthentication();

在登录程序中验证通过用户名/密码后,通过下面的代码生成登录 Cookie 并发送给客户端:

var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, model.Email) }, "Basic");
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
claimsPrincipal);

最新文章

  1. iOS Real Stuff
  2. document.cookie的使用
  3. boostrap中lg,md,sm,xs
  4. 【M8】了解各种不同意义的new和delete
  5. corejava-chap01
  6. SE 2014年5月9日
  7. bat文件无法双击运行
  8. Windows Phone 8 MD5
  9. 关于Two-Pass标记连通域个数
  10. Spring MVC CORS 跨域
  11. 什么是web框架
  12. spark streamingcontext
  13. socket实现FTP上传下载功能
  14. Linux_问题
  15. fpm...failed: Cannot assign requested address
  16. 洛谷P2057 【SHOI2007】善意的投票
  17. python类的全面介绍
  18. ob系列函数中常用函数
  19. 如何使用JDBC删除一条记录
  20. POJ3261(SummerTrainingDay10-G 后缀数组)

热门文章

  1. Objc的底层并发API
  2. .NET 同步与异步 之 警惕闭包(十)
  3. SSD卡对mongodb的影响
  4. Coding in Delphi(前4章翻译版本) (PDF)
  5. idea svn 不见的问题
  6. Socket阻塞模式和非阻塞模式的区别
  7. js调用winform程序(带参数)
  8. [Angularjs] 第一步开始一个项目
  9. Fedora 21 安装 Budgie Desktop
  10. Java——安全地停止线程