在asp.net core 2.0

时间:2017-11-03 16:42:00

标签: identityserver4 asp.net-core-2.0

我有一个使用identityserver4框架的Identity Server,其网址是http://localhost:9000

我的网络应用程序是asp.net core 2.0,其网址是http://localhost:60002。此应用程序将使用Identity Server的登录页面。

我想在登录后,Identity Server将重定向到应用程序页面(http://localhost:60002

以下是客户端应用程序的Startup.cs

Startup.cs

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

        public IConfiguration Configuration { get; }

        private string AuthorityUri => Configuration.GetValue<string>("UserManagement-Authority");

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

            services.AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddOpenIdConnect(options =>
            {
                options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.Authority = AuthorityUri; // "http://localhost:9000"
                options.RequireHttpsMetadata = false;
                options.ClientId = "customer.api";
                options.ClientSecret = "testsecret";
                options.ResponseType = "code id_token";
                options.Scope.Add("customerprivatelinesvn.api");
                options.Scope.Add("offline_access");
                options.GetClaimsFromUserInfoEndpoint = true;
                options.SaveTokens = true;
            });

            services.AddMvc();
        }

        // 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.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
                {
                    HotModuleReplacement = true
                });
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();            

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

                routes.MapSpaFallbackRoute(
                    name: "spa-fallback",
                    defaults: new { controller = "Home", action = "Index" });
            });
        }
    }

以下是Identity Server上的登录页面

enter image description here

但是有一个无限循环调用http://localhost:9000/connect/authorize端点,然后它返回http://localhost:60002/signin-oidc并显示“Bad Request - Request Too Long”,如下所示。

当我查看cookies时,会有很多项目“.AspNetCore.Correlation.OpenIdConnect.xxx” enter image description here

以下是Identiy Server上的日志。它说Identiy.Application已成功通过身份验证。 enter image description here

有谁知道这个问题是什么?以及如何解决这个问题?非常感谢你。

致以最诚挚的问候,

凯文

6 个答案:

答案 0 :(得分:1)

从现有的.NET Core 2.2项目复制启动代码并在新的.NET Core 3.1项目中重新使用启动代码后,我也遇到了登录循环。

这里的问题是必须在新的app.UseAuthorization()之前调用app.UseAuthentication();

https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#migrate-startupconfigure

仅当有人也遇到此问题时...

答案 1 :(得分:0)

嗯,您的Identity Server日志中确实显示了很长的请求 - 错误显示“错误请求 - 请求时间过长”。我猜这个问题是你的请求太大了:) maximum length of HTTP GET request?

您是否尝试过发帖而不是使用GET?

答案 2 :(得分:0)

在您的客户端应用中,在启动时检查您是否有类似

的内容
services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

删除该部分,然后重试。

答案 3 :(得分:0)

就我而言,从客户端启动登录时我丢失了 RedirectUri 。通过添加如下所示的RedirectUri解决了问题。

 public IActionResult SignIn()
        {

            return Challenge(new AuthenticationProperties() { RedirectUri = "/" }, "oidc" );
        }

答案 4 :(得分:0)

我更新了IdentityServer4和.NET Core的最新nuget包后,此问题已解决。

答案 5 :(得分:0)

在客户端应用中添加默认身份会导致无限重定向循环。

在客户端应用中,如果需要使用UserManager和RoleManager。

然后使用下面的代码。

services.AddIdentityCore<IdentityUser>()
                .AddRoles<IdentityRole>()
                .AddRoleManager<RoleManager<IdentityRole>>()
                .AddSignInManager<SignInManager<IdentityUser>>()
                .AddEntityFrameworkStores<ApplicationDbContext>();