设为首页 加入收藏

TOP

微服务(入门四):identityServer的简单使用(客户端授权+密码授权)(二)
2019-09-17 18:44:25 】 浏览:58
Tags:服务 入门 identityServer 简单 使用 客户端 授权 密码
r4.Models;
using IdentityServer4.Test; using IdentityServer4Test.IndntityConfig; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace IdentityServer4Test { 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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); // 在DI容器中注入identityServer服务 services.AddIdentityServer() .AddInMemoryApiResources(IdentityServerConfig.GetResources())//添加配置的api资源 .AddInMemoryClients(IdentityServerConfig.GetClients())//添加客户端,定义一个可以访问此api的客户端 .AddDeveloperSigningCredential(); } // 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(); } //添加identityserver中间件到http管道 app.UseIdentityServer(); //app.UseMvc(); } } }

5.此时启动程序输入 http://localhost:3322/.well-known/openid-configuration可以得到如下网站,这是identity Server4 提供的配置文档

6.用postMan请求获取access_token

  

标注:body 当中的参数

        grant_type    :对应api AllowedGrantTypes 类型表示授权模式

        client_id        : 对应clentID 

        client_secret: 客户端秘钥

       

7.拿到token以后就可以根据token去访问我们的服务程序,服务程序,首先也是创建一个webApi程序,并且从NuGet下载所需的依赖

  •    IdentityServer4.AccessTokenValidation(程序报管理器的话加上install-package IdentityServer4.AccessTokenValidation)

  7.1 配置authentication,并且添加    app.UseAuthentication();到http管道当中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace IndentityServerClientTest
{
    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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
             //注入authentication服务
            services.AddAuthentication("Bearer")
            
               .AddIdentityServerAuthentication(options =>
               {
                   options.Authority = "http://localhost:3322";//IdentityServer服务地址
                   options.ApiName = "api1"; //服务的名称,对应Identity Server当中的Api资源名称
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇第15章 使用EntityFramework Core.. 下一篇第4章 打包和构建 - Identity Ser..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目