Asp.NetCoreAuthorize解析(源码解读)

Asp.NetCoreAuthorize解析(源码解读)

2023年6月20日发(作者:)

eAuthorize解析(源码解读)⼀、前⾔IdentityServer4已经分享了⼀些应⽤实战的⽂章,从架构到授权中⼼的落地应⽤,也伴随着对IdentityServer4掌握了⼀些使⽤规则,但是很多原理性东西还是⼀知半解,故我这⾥持续性来带⼤家⼀起来解读它的相关源代码,本⽂先来看看为什么Controller或者Action中添加Authorize或者全局中添加AuthorizeFilter过滤器就可以实现该资源受到保护,需要通过access_token才能通过相关的授权呢?今天我带⼤家来了解AuthorizeAttribute和AuthorizeFilter的关系及代码解读。⼆、代码解读解读之前我们先来看看下⾯两种标注授权⽅式的代码:标注⽅式111213 [Authorize] [HttpGet] public async Task Get() { var userId = (); return new { name = (), userId = userId, displayName = yName(), merchantId = ntId(), }; }代码中通过[Authorize]标注来限制该api资源的访问全局⽅式11121314public void ConfigureServices(IServiceCollection services){ //全局添加AuthorizeFilter 过滤器⽅式 trollers(options=>(new AuthorizeFilter()));

horization(); hentication("Bearer") .AddIdentityServerAuthentication(options => { ity = "localhost:5000"; //配置Identityserver的授权地址 eHttpsMetadata = false; //不需要https

e = e; //api的name,需要和config的名称相同 });}全局通过添加AuthorizeFilter过滤器⽅式进⾏全局api资源的限制AuthorizeAttribute先来看看AuthorizeAttribute源代码:8[AttributeUsage( | , AllowMultiple = true, Inherited = true)]public class AuthorizeAttribute : Attribute, IAuthorizeData{ ///

/// Initializes a new instance of the class.

///

public AuthorizeAttribute() { }

///

/// Initializes a new instance of the class with the specified policy.

///

/// The name of the policy to require for authorization. public AuthorizeAttribute(string policy) { Policy = policy; }

///

/// 收取策略 /// public string Policy { get; set; }

///

/// 授权⾓⾊ /// public string Roles { get; set; }

///

发布者:admin,转转请注明出处:http://www.yc00.com/web/1687249614a16.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信