• 五、通过密码访问API


    通过密码访问API

    一、客户端

    图:

    客户端请求代码:

            static void Main(string[] args)
            {
                Console.WriteLine("确定三个项目都已经启动");
                Console.Read();
                Console.WriteLine("按任意键开始运行");
                // discover endpoints from metadata
                var client = new HttpClient();
                var disco = client.GetDiscoveryDocumentAsync("http://localhost:5000").ConfigureAwait(false).GetAwaiter().GetResult();
                if (disco.IsError)
                {
                    Console.WriteLine(disco.Error);
                    return;
                }
                // request token
                var tokenResponse = client.RequestPasswordTokenAsync(new PasswordTokenRequest
                {
                    Address = disco.TokenEndpoint,//默认
                    ClientId = "socialnetwork",
                    ClientSecret = "secret",
                   //GrantType = "password",//这句话可以加也可以不加 与资源服务器对应 AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                    UserName = "mail@qq.com",
                    Password = "password",
                    Scope = "socialnetwork",
                }).ConfigureAwait(false).GetAwaiter().GetResult();
                // call api
                client.SetBearerToken(tokenResponse.AccessToken);
                var response = client.GetAsync("http://localhost:5001/identity").ConfigureAwait(false).GetAwaiter().GetResult();
                if (!response.IsSuccessStatusCode)
                {
                    Console.WriteLine(response.StatusCode);
                }
                else
                {
                    var content =  response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
                    Console.WriteLine(JArray.Parse(content));
                }
                Console.ReadKey(false);//因为控制台会关闭,设置不关闭     
            }

    二、颁发token服务器

    服务端配置文件必须添加

        public class Startup
        {
            // This method gets called by the runtime. Use this method to add services to the container.
            // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
            public void ConfigureServices(IServiceCollection services)
            {
                var builder = services.AddIdentityServer()
               .AddDeveloperSigningCredential()
               .AddInMemoryIdentityResources(Config.GetIdentityResources())
               .AddInMemoryApiResources(Config.ApiResources())
               .AddInMemoryClients(Config.Clients())//添加客户端
               .AddTestUsers(Config.Users().ToList());//添加用户支持
                // rest omitted
            }
            // 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();
                }
    
                app.UseIdentityServer();
    
                app.Run(async (context) =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            }
        }

    如果不加则是

  • 相关阅读:
    SQL Server 存储过程
    SQL数据类型介绍
    XML文件操作指南
    C# 程序集 与 反射
    详解C#中System.IO.File类和System.IO.FileInfo类的用法
    C# 之泛型详解
    C#中HashTable的用法示例2
    C#中HashTable的用法示例1
    DEDECMS5.7 首页和栏目页调用文章按权重排序
    linux创建用户,指定组
  • 原文地址:https://www.cnblogs.com/fger/p/11045894.html
Copyright © 2020-2023  润新知