代码之家  ›  专栏  ›  技术社区  ›  Mike Becatti

OpenIdDict-对XHR请求禁用“连接/授权”重定向

  •  1
  • Mike Becatti  · 技术社区  · 8 年前

    我正在使用openIdDict的示例MVC应用程序来实现授权代码流。但是,我有一个angular 6应用程序,用于用户授权请求的登录页。我已经让所有的angular工具都工作了,但是当我提交“连接/授权”请求时,它会生成一个302。302被浏览器捕获,重定向正在发生,但我不想这样。我希望请求以200的形式出现,然后angular应用程序可以从那里控制重定向。

    我想这样做的原因: 1.我正在集成的系统需要在重定向中填充额外的查询字符串参数(state*others)。我希望我的angular应用程序填充这些内容,而不是连接/授权。 2.angular应用程序将在用户允许授权后但在重定向发生之前向用户提供额外的说明/信息。

    我的问题是:

    启动。cs配置

                services.AddOpenIddict()
                .AddCore(coreOptions =>
                {
                    coreOptions.UseEntityFrameworkCore().UseDbContext<ApplicationDbContext>();
                })
                .AddServer(serverOptions =>
                {
                    serverOptions.UseMvc();
    
                    serverOptions.EnableAuthorizationEndpoint(oauthOptions.AuthorizePath)
                    .EnableTokenEndpoint(oauthOptions.TokenPath)
                           .EnableLogoutEndpoint(oauthOptions.LogoutPath);
    
                    serverOptions.AllowAuthorizationCodeFlow()
                           .AllowPasswordFlow()
                           .AllowRefreshTokenFlow();
    
                    serverOptions.SetAccessTokenLifetime(new TimeSpan(0, oauthOptions.AccessTokenLifetimeMinutes, 0));
                    serverOptions.SetRefreshTokenLifetime(new TimeSpan(0, oauthOptions.RefreshTokenLifetimeMinutes, 0));
    
                    if (!oauthOptions.RequireSSL)
                    {
                        serverOptions.DisableHttpsRequirement();
                    }
    
                });
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Mike Becatti    8 年前

    我决定编写自己的中间件,拦截Identity生成的302请求,并用JSON主体的200请求替换它。我相信这可以简化,也许我只是错过了一些重要的东西,我偏离了方向。底线是这个解决方案允许用户使用ajax实现授权代码流。

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            //put this before any other middleware runs
            app.UseMiddleware<AuthorizeRequestMiddleware>();
            ...
        }
    

    授权请求中间件

    public class AuthorizeRequestMiddleware
    {
        private readonly RequestDelegate _next;
    
        public AuthorizeRequestMiddleware(RequestDelegate next)
        {
            _next = next;
        }
    
        public async Task Invoke(HttpContext context)
        {
            context.Response.OnStarting(AuthorizeStartingHandler, state: context);
    
            // Let the middleware pipeline run
            await _next(context);
        }
    
        private Task AuthorizeStartingHandler(object context)
        {
    
            HttpContext httpContext = (HttpContext)context;
            if (httpContext.Request.Path.HasValue && httpContext.Request.Path.Value == "/connect/authorize")
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.OK;
                AuthorizationCodeResponse responseBody = new AuthorizationCodeResponse(httpContext.Response.Headers["Location"][0]);
                httpContext.Response.WriteAsync(JsonConvert.SerializeObject(responseBody));
            }
    
            return Task.CompletedTask;
        }
    }
    

    授权代码响应 这是我自己的课。您可以使响应看起来像您想要的那样。

    public class AuthorizationCodeResponse
    {
        public AuthorizationCodeResponse(string redirectUri)
        {
            this.RedirectUri = redirectUri;
        }
    
        public string RedirectUri { get; set; }
    }
    
        2
  •  0
  •   Kévin Chalet    8 年前

    services.AddOpenIddict()
        .AddCore(options =>
        {
            // ...
        })
    
        .AddServer(options =>
        {
            // ...
            options.EnableRequestCaching();
        });
    

    启用时,此功能将授权请求存储在 IDistributedCache 并将授权请求参数替换为单个 request_id

    如果你不喜欢这种行为,就不要打电话 options.EnableRequestCaching() 在OpenIddict服务器选项中。