.net core 学习小结之 Cookie-based认证
2024-09-05 17:15:17
- 在startup中添加授权相关的管道
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; namespace mvcforcookie
{
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication.Cookies;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(option => option.LoginPath = "/Acounnt/Index");
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
} - 将需要权限访问的页面贴上特性标签
[Authorize(Roles="Admin")] 表名只有Admin身份的人才能进入Admin控制器
- 用户成功输入用户名和密码之后生成用户票据
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using mvcforcookie.Models; namespace mvcforcookie.Controllers
{
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using System.Security.Claims;
public class AcounntController : Controller
{
public IActionResult Index()
{
//数据库查询用户输入的用户名和密码等一系列匹配操作
//模拟用户登录后的操作
//创建一个用户身份
var claims=new List<Claim>{
new Claim(ClaimTypes.Name,"cyao"),
new Claim(ClaimTypes.Role,"Admin")
};
var claimidentity=new ClaimsIdentity(claims,CookieAuthenticationDefaults.AuthenticationScheme);
//向上下文容器中添加当前用户
HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,new ClaimsPrincipal(claimidentity));
return Ok();
}
public IActionResult LoginOut()
{
HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return Ok();
}
}
} - 如果要获取当前用户的身份和用户名的话
ViewBag.User= User.Claims.Where(c =>c.Type==ClaimTypes.Name).First().Value;
ViewBag.Type= User.Claims.Where(c =>c.Type==ClaimTypes.Role).First().Value;
最新文章
- Spring的3种切入点PointCut实现
- windows服务
- paper 106:图像增强方面的介绍
- js格式化数字和金额
- atitit.标准时间格式 互相转换 秒数 最佳实践
- linux tricks 之 typeof用法.
- FlyCapture2 VS2010 Configuration
- DOM 中 Property 和 Attribute 的区别
- 2013 Multi-University Training Contest 1 Cards
- 遍历map的四方方法
- java基础之导入(药师点评)
- 图片拉伸(有保护区域) resizableImageWithCapInsets
- UML它 时序图
- Servlet支持上传多张图片
- 如何使用.bas文件
- BootKit病毒——“异鬼Ⅱ”的前世今生
- 201521123002 《Java程序设计》第4周学习总结
- Java进阶篇设计模式之十二 ---- 备忘录模式和状态模式
- redis介绍、安装、redis持久化、redis数据类型
- 1. cs231n k近邻和线性分类器 Image Classification
热门文章
- 由n个已按1,2,3,.....,n编号的人围成一圈,从编号为1的人开始按1,2,3顺序循环报数,凡报到3者出圈,最后留一个人,问其编号是多少
- pyinstaller 打包工具的使用方法
- swiper保存
- 查看是否安装jdk及路径
- 19.顺时针打印矩阵(python)
- py从入门到实践 第四章
- #419 Div2 Problem C Karen and Game (贪心 &;&; 暴力)
- Devexpress MVC Gridview
- luogu P1083 借教室 x
- 苹果CMSv10对接微信公众号教程