Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IdentityServer4管理用户界面_C#_Security_Access Token_Identityserver4 - Fatal编程技术网

C# IdentityServer4管理用户界面

C# IdentityServer4管理用户界面,c#,security,access-token,identityserver4,C#,Security,Access Token,Identityserver4,我正在开发在github上开发的IdentityServer4.AdminUI 首先,我简单地创建了一个新用户并设置了密码,然后我创建了一个名为Api\u name的新ApiResource。然后我创建了同名的IdentityResourceApi\u name。最后,我创建了一个名为Api\u-client的新客户端,并将客户端允许的作用域设置为Api\u-name,允许的授权类型设置为Password,最后将客户端密码设置为secret 现在,我创建了新的WebApi项目(Core2.1)

我正在开发在github上开发的IdentityServer4.AdminUI

首先,我简单地创建了一个新用户并设置了密码,然后我创建了一个名为Api\u name的新ApiResource。然后我创建了同名的IdentityResourceApi\u name。最后,我创建了一个名为Api\u-client的新客户端,并将客户端允许的作用域设置为Api\u-name,允许的授权类型设置为Password,最后将客户端密码设置为secret

现在,我创建了新的WebApi项目(Core2.1),并在startup类中使用它

public void ConfigureServices(IServiceCollection services) {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddMvcCore().AddAuthorization().AddJsonFormatters();

        services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options => {
                options.Authority = "http://localhost:5000"; //Identity Server URL
                options.RequireHttpsMetadata = false; // make it false since we are not using https
                options.ApiName = "Api_Name"; //api name which should be registered in IdentityServer
            });
    }

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

        app.UseHttpsRedirection();
        app.UseMvc();
    }
当然我在WebApi控制器中使用了[Authorize]属性

最后是测试。 我创建了控制台应用程序并使用此代码

var identityServer = await DiscoveryClient.GetAsync("http://localhost:5000"); //discover the IdentityServer
        if (identityServer.IsError) {
            Console.Write(identityServer.Error);
            return;
        }

        HttpClient client = new HttpClient();

        var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest {
            Address = identityServer.TokenEndpoint,
            ClientId = "Api_Client",
            ClientSecret = "secret",

            UserName = "Majd",
            Password = "P@ssw0rd@123"
        });

        if (tokenResponse.IsError) {
            Console.WriteLine(tokenResponse.Error);
            return;
        }

        //Call the API

        client.SetBearerToken(tokenResponse.AccessToken);

        var response = await client.GetAsync("https://localhost:44368/api/values");
        var response2 = await client.GetAsync("https://localhost:44368/api/values/1");
        var content = await response.Content.ReadAsStringAsync();
        Console.WriteLine(JArray.Parse(content));
        Console.ReadKey();

问题是响应2返回未经授权的401。既然我使用了从identity server收到的访问令牌,那么为什么会出现此错误呢?您还需要在令牌请求中添加一个请求的作用域(即使您说允许客户端访问
Api\u Name


在IDS4中,令牌仅针对已请求的作用域发出,而不像IDS3,在IDS3中,您将获得客户端允许的所有作用域。因此,就您的Api身份验证中间件而言,您的客户端不允许访问它,因为令牌不够。

响应的返回结果如何??响应从api返回值,但未授权。我把它放在确保每件事都是O.KDid的位置上,您尝试将用户添加到一个角色,并将对该角色的访问权限授予API…确切地说,这是缺少的。我应该为我的ApiResource添加一个作用域,并在客户端中授予对此作用域的访问权。
    var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest {
        Address = identityServer.TokenEndpoint,
        ClientId = "Api_Client",
        ClientSecret = "secret",

        UserName = "Majd",
        Password = "P@ssw0rd@123",
        Scope = "Api_Name"
    });