zoukankan      html  css  js  c++  java
  • 五、通过密码访问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!");
                });
            }
        }

    如果不加则是

  • 相关阅读:
    叶落归根(hometown)
    设置(settings)
    文明距离(civil)
    计算机基础知识
    gojs插件使用教程
    编程语言分类
    dp优化简单总结
    Splay入门题目 [HNOI2002]营业额统计
    hdu3415:最大k子段和,单调队列
    hdu5072(鞍山regional problem C):容斥,同色三角形模型
  • 原文地址:https://www.cnblogs.com/fger/p/11045894.html
Copyright © 2011-2022 走看看