首页 > 其他 > 详细

IdentityServer4学习(一)定义身份资源

时间:2021-04-09 13:24:07      阅读:29      评论:0      收藏:0      [点我收藏+]
定义资源(IdentityResource)
两种基本资源
1.身份资源:代表有关用户的声明,例如用户ID,显示名称,电子邮件地址等
2.Api资源 代表客户端想要访问的功能
 
IResourceStore 可以实现这个接口 做到从数据存储中定义资源 (官网示例文档是从内存中加载资源)
public static IEnumerable<IdentityResource> GetIdentityResources()
{
    return new List<IdentityResource>
    {
        new IdentityResource(
            name: "openid",
            userClaims: new[] { "sub" },
            displayName: "Your user identifier")
    };
}

  

简写:
public static IEnumerable<IdentityResource> GetIdentityResources()
{
    return new List<IdentityResource>
    {
        new IdentityResources.OpenId()
    };
}

 

 
(从内存中获取)
 public class ResourceStore : IResourceStore
 {
        /// <summary>
        /// The DbContext.
        /// </summary>
        protected readonly IConfigurationDbContext Context;

        /// <summary>
        /// The logger.
        /// </summary>
        protected readonly ILogger<ResourceStore> Logger;

        /// <summary>
        /// Initializes a new instance of the <see cref="ResourceStore"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="logger">The logger.</param>
        /// <exception cref="ArgumentNullException">context</exception>
        public ResourceStore(IConfigurationDbContext context, ILogger<ResourceStore> logger)
        {
            Context = context ?? throw new ArgumentNullException(nameof(context));
            Logger = logger;
        }
        ...
 }

 

(从数据库获取资源)
 
自定义身份资源
 
定义一个名称为profile自定义身份资源
public static IEnumerable<IdentityResource> GetIdentityResources()
{
    return new List<IdentityResource>
    {
        new IdentityResource(
            name: "profile",
            userClaims: new[] { "name", "email", "website" },
            displayName: "Your profile data")
    };
}

 

定义资源后,可以通过AllowedScopes 将访问权限授予客户端
var client = new Client
{
    ClientId = "client",

    AllowedScopes = { "openid", "profile" }
};

 

客户端可以使用scope参数请求资源
例如 使用vue + oidc-client
 
constructor () {
    super({
      authority: ‘http://localhost:5000/‘,
      client_id: ‘vuejs‘,
      redirect_uri: ‘http://192.168.1.26:8081/callback‘,
      response_type: ‘id_token token‘,
      scope: ‘openid profile roles ‘,
      post_logout_redirect_uri: ‘http://192.168.1.26:8081‘
    })
  }

 

 
https://http://localhost:5000/connect/authorize?client_id=client&scope=openid profile
 
Next(Api资源定义)

IdentityServer4学习(一)定义身份资源

原文:https://www.cnblogs.com/827648168yj/p/14635663.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!