【问题标题】:Can't access client application无法访问客户端应用程序
【发布时间】:2019-01-20 16:25:59
【问题描述】:

此应用程序将 Identity 4 与客户端 mvc 应用程序和 IDP(身份提供者)asp.net 核心 Web 应用程序一起使用。

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

如何解决这个问题????

IDP项目启动项目(localhost:44393)

 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)
        {
            services.AddMvc();

            services.AddIdentityServer()
                  .AddDeveloperSigningCredential()
                  .AddTestUsers(Config.GetUsers())
                  .AddInMemoryIdentityResources(Config.GetIdentityResources())
                  .AddInMemoryClients(Config.GetClients());

        }

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

            app.UseMvcWithDefaultRoute();
            //app.Run(async (context) =>
            //{
            //    await context.Response.WriteAsync("Hello World!");
            //});
        }
    }

配置.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())
                }

                }
            };
        }

    }

客户端应用程序(本地主机:44335)

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            services.AddAuthentication(options =>
            {
                options.DefaultScheme = "Cookies";
                options.DefaultChallengeScheme = "oidc";
            }).AddCookie("Cookies",
           (options) =>
           {

           }).AddOpenIdConnect("oidc", options => {
               options.SignInScheme = "Cookies";
               options.Authority = "https://localhost:44393";
               options.ClientId = "imagegalleryclient";
               options.ResponseType = "code id_token";
               options.SaveTokens = true;
               options.ClientSecret = "secret";

           });


        }

        // 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.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseAuthentication();

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{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}");
            }
        }
    }

首先使用用户名和密码登录后

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

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc asp.net-identity asp.net-core-2.0


    【解决方案1】:

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

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

    【讨论】:

    • 我是 asp .net 核心身份服务器的新人 4.如何向应用程序添加新范围。以及如何配置它??
    • @KasunjithBimal 这是您的Configure.cs 的摘录,只需将额外的行IdentityServerConstants.StandardScopes.Profile 添加到AllowedScopes 属性即可。
    • 我将这一行添加到我的代码中。现在我显示 Image Galary 正在请求您的许可页面,但没有在我的控制器类中达到我的断点。
    • @KasunjithBimal 好吧,您需要授予权限,然后它将被重定向回您的应用程序和您的控制器。
    • @KasunjithBimal 你现在最好问另一个问题。这个答案已经解决了你在描述中的问题。如果没有适当的上下文,很难理解您现在遇到了什么问题。
    猜你喜欢
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    相关资源
    最近更新 更多