C# Can';t访问客户端应用程序

C# Can';t访问客户端应用程序,c#,asp.net-core,asp.net-core-mvc,asp.net-identity,asp.net-core-2.0,C#,Asp.net Core,Asp.net Core Mvc,Asp.net Identity,Asp.net Core 2.0,此应用程序将Identity 4与客户端mvc应用程序和IDP(Identity provider)asp.net核心web应用程序一起使用 无法访问控制器索引操作视图 如何解决这个问题 IDP项目启动项目(本地主机:44393) 配置.cs文件 public static class Config { public static List<TestUser> GetUsers() { return new List&

此应用程序将Identity 4与客户端mvc应用程序和IDP(Identity provider)asp.net核心web应用程序一起使用

无法访问控制器索引操作视图

如何解决这个问题

IDP项目启动项目(本地主机:44393)

配置.cs文件

 public static class Config
    {
        public static List<TestUser> GetUsers()
        {
            return new List<TestUser>
            {
                new TestUser
                {
                    SubjectId ="d866oef",
                    Username ="Kasunjith",
                    Password="password",
                    Claims= new List<Claim>
                    {
                        new Claim("given_name","Kasunjith"),
                        new Claim("family_name","Underwood"),
                    }
                }, new TestUser
                {
                    SubjectId ="d866omf",
                    Username ="BimalJith",
                    Password="password",
                    Claims= new List<Claim>
                    {
                        new Claim("given_name","BimalJith"),
                        new Claim("family_name","ViewWord"),
                    }
                },

            };

        }
        // identity-related resources (Scopes)
        public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile()
            };
        }

        public static IEnumerable<Client> GetClients()
        {
            return new List<Client>()
            {
                new Client
                {
                ClientName="Image Galary",
                ClientId="imagegalleryclient",
                AllowedGrantTypes = GrantTypes.Hybrid,
                RedirectUris = new List<string>()
                {
                    "https://localhost:44335/signin-oidc"
                },
                AllowedScopes =
                {
                        IdentityServerConstants.StandardScopes.OpenId
                },
                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                }

                }
            };
        }

    }
公共静态类配置
{
公共静态列表GetUsers()
{
返回新列表
{
新测试用户
{
SubjectId=“d866oef”,
Username=“Kasunjith”,
Password=“Password”,
索赔=新名单
{
新索赔(“给定名称”、“Kasunjith”),
新索赔(“姓”、“安德伍德”),
}
},新测试用户
{
SubjectId=“d866omf”,
Username=“BimalJith”,
Password=“Password”,
索赔=新名单
{
新索赔(“给定名称”、“BimalJith”),
新索赔(“家族名称”、“ViewWord”),
}
},
};
}
//与身份相关的资源(作用域)
公共静态IEnumerable GetIdentityResources()
{
返回新列表
{
新标识资源.OpenId(),
新的IdentityResources.Profile()
};
}
公共静态IEnumerable GetClients()
{
返回新列表()
{
新客户
{
ClientName=“Image Galary”,
ClientId=“imagegalleryclient”,
AllowedGrantTypes=GrantTypes.Hybrid,
重定向URI=新列表()
{
"https://localhost:44335/signin-oidc“
},
允许范围=
{
IdentityServerConstants.StandardScopes.OpenId
},
客户秘密=
{
新密码(“Secret.Sha256())
}
}
};
}
}
客户端应用程序(本地主机:44335)

公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
services.AddSingleton();
services.AddAuthentication(选项=>
{
options.DefaultScheme=“Cookies”;
options.DefaultChallengeScheme=“oidc”;
}).AddCookie(“Cookies”,
(选项)=>
{
}).AddOpenIdConnect(“oidc”,选项=>{
options.signnscheme=“Cookies”;
选项。权限=”https://localhost:44393";
options.ClientId=“imagegalleryclient”;
options.ResponseType=“代码id\U令牌”;
options.SaveTokens=true;
options.ClientSecret=“secret”;
});
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseAuthentication();
app.UseStaticFiles();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller=Gallery}/{action=Index}/{id?}”);
});
}
}
我的控制器类

 [Authorize]
    public class GalleryController : Controller
    {
        public async Task<IActionResult> Index()
        {
            await WriteOutIdentityInformation();
            return View();
        }


        public async Task WriteOutIdentityInformation()
        {
            var identityToken = await HttpContext.GetTokenAsync(OpenIdConnectParameterNames.IdToken);


            Debug.WriteLine($"Identity token:{identityToken}");

            foreach (var claim in User.Claims)
            {
                Debug.WriteLine($"Claim type:{ claim.Type} -Claim value : {claim.Value}");
            }
        }
    }
[授权]
公共类GalleryController:控制器
{
公共异步任务索引()
{
等待WriteOutIdentityInformation();
返回视图();
}
公共异步任务WriteOutIdentityInformation()
{
var identityToken=await-HttpContext.GetTokenAsync(OpenIdConnectParameterNames.IdToken);
WriteLine($“标识令牌:{identityToken}”);
foreach(User.Claims中的var声明)
{
Debug.WriteLine($“声明类型:{Claim.type}-声明值:{Claim.value}”);
}
}
}
使用用户名和密码登录后的第一次

之后转到localhost:44335/Gallary/index显示此错误

对此不是100%确定,但我认为默认情况下,
AddOpenIdConnect
将请求
OpenId
Profile
范围,但是,您只授予您的客户
OpenId
范围,因此需要添加另一个范围

            AllowedScopes =
            {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile
            },

我是asp.net core identity server 4的新手。如何向应用程序添加新作用域以及如何配置它???@KasunjithBimal这是从您的
configure.cs
中摘录的,只需在
AllowedScopes
属性中添加额外的行
IdentityServerConstants.StandardScopes.Profile
。我将这一行添加到我的代码中。现在我显示Image Galary正在请求您的权限页,但在我的控制器类中没有达到我的断点。@KasunjithBimal您需要授予权限,然后它将重定向回您的应用程序和控制器。@KasunjithBimal您最好现在再问另一个问题。这个答案解决了描述中的问题。如果没有适当的背景,很难理解你现在遇到了什么问题。
 [Authorize]
    public class GalleryController : Controller
    {
        public async Task<IActionResult> Index()
        {
            await WriteOutIdentityInformation();
            return View();
        }


        public async Task WriteOutIdentityInformation()
        {
            var identityToken = await HttpContext.GetTokenAsync(OpenIdConnectParameterNames.IdToken);


            Debug.WriteLine($"Identity token:{identityToken}");

            foreach (var claim in User.Claims)
            {
                Debug.WriteLine($"Claim type:{ claim.Type} -Claim value : {claim.Value}");
            }
        }
    }
            AllowedScopes =
            {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile
            },