美文网首页
基于IdentityServer4的单点登录——Api

基于IdentityServer4的单点登录——Api

作者: Lulus | 来源:发表于2017-12-05 14:06 被阅读0次

    1.新建项目并添加引用

    新建一个asp .net core 2.0的项目
    引用IdentityServer4.AccessTokenValidation

    2.配置

    将Api与IdentityServer服务器挂钩

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvcCore()
                .AddAuthorization()
                .AddJsonFormatters();
    
            services.AddAuthentication("Bearer")
                .AddIdentityServerAuthentication(options =>
                {
                    //这里添加IdentityServer服务器的地址
                    options.Authority = "http://localhost:5000";
                    options.RequireHttpsMetadata = false;
                    //这个Api资源的名称,注意与IdentityServer的Api资源对应
                    options.ApiName = "api1";
                });
        }
    
        public void Configure(IApplicationBuilder app)
        {
            app.UseAuthentication();
    
            app.UseMvc();
        }
    }
    

    3.Api接口

    添加一个新的Controller,使用此控制器来测试授权要求,以及通过Api可视化声明身份

    [Route("identity")]
    [Authorize]
    public class IdentityController : ControllerBase
    {
        [HttpGet]
        public IActionResult Get()
        {
            return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
        }
    }
    

    4.设置Api项目的端口号为5001

    相关文章

      网友评论

          本文标题:基于IdentityServer4的单点登录——Api

          本文链接:https://www.haomeiwen.com/subject/eofcixtx.html