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!");
                });
            }
        }

    如果不加则是

  • 相关阅读:
    python中读取文件数据时要注意文件路径
    sklearn.model_selection 的 train_test_split作用
    matplotlib中subplot的各参数的作用
    用梯度下降算法求最值
    AfxMessageBox与MessageBox用法与区别
    MFC、API、C++三者的区别
    2、CString与string借助char *互转
    1、创建MFC应用程序——单个文档
    1、Mat类的属性、方法
    CMake编译OpenCV
  • 原文地址:https://www.cnblogs.com/fger/p/11045894.html
Copyright © 2011-2022 走看看