IdentityServer4 密码模式实现

1.  修改 Config.cs 

using System.Collections;
using System.Collections.Generic;
using IdentityServer4.Models;
using IdentityServer4.Test;//测试的时候使用

namespace IdentityServiceSample
{
    public class Config
    {
        public static IEnumerable<ApiResource> GetResource()
        {

            return new List<ApiResource>(){
                new ApiResource("api","my api")
            };

        }

        public static IEnumerable<Client> GetClients()
        {

            return new List<Client>(){
                new Client(){
                    ClientId="client",
                    //客户端模式
                     AllowedGrantTypes=GrantTypes.ClientCredentials,
                     ClientSecrets={new Secret("secret".Sha256())},
                     AllowedScopes={"api"}
                },
                new Client(){
                    ClientId="pwdClient",
                    //OAuth密码模式
                     AllowedGrantTypes=GrantTypes.ResourceOwnerPassword,

                     ClientSecrets={new Secret("secret".Sha256())},

                     AllowedScopes={"api"}
                }
            };
        }

        //TODO 实际使用可以读取Db 
        public static List<TestUser> GetTestUser()
        {
            return new List<TestUser>{
                new TestUser{
                    SubjectId="1",
                    Username="sunxuchu",
                    Password="123456"
                }
            };
        }
    }
}

2. 修改 Startup.cs

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

namespace IdentityServiceSample
{
    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)
        {
            //1.注入IdentityServer
            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryApiResources(Config.GetResource())
            .AddInMemoryClients(Config.GetClients())
            .AddTestUsers(Config.GetTestUser());

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // 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.UseHsts();
            }

            // app.UseHttpsRedirection();
            // app.UseMvc();

            //2. 注册IdentityServer
            app.UseIdentityServer();
        }
    }
}

3. 使用PostMan 调用调试

请求参数:

client_id:pwdClient
client_secret:secret
grant_type:password
password:123456
username:sunxuchu

 

4. 密码模式HttpClient 模拟请求

using System;
using System.Net.Http;
using IdentityModel.Client;

namespace PwdClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                new Program().GetAsync();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }

        public async void GetAsync()
        {
            var diso = await DiscoveryClient.GetAsync("http://localhost:5003");
            if (diso.IsError)
            {
                System.Console.WriteLine("diso.Error");
            }
            var tokenClient = new TokenClient(diso.TokenEndpoint, "pwdClient", "secrt");
            var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync("sunxuchu","123456");
            if (tokenResponse.IsError)
            {
                System.Console.WriteLine(tokenResponse.Error);
            }
            else
            {
                System.Console.WriteLine(tokenResponse.Json);
            }

            using (var httpClient = new HttpClient())
            {
                httpClient.SetBearerToken(tokenResponse.AccessToken);
                var response = await httpClient.GetAsync("http://localhost:5001/api/values");
                if (response.IsSuccessStatusCode)
                {
                    System.Console.WriteLine(await response.Content.ReadAsStringAsync());
                }
            }

        }
    }
}

 

转载于:https://www.cnblogs.com/sunxuchu/p/9251885.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值