CORS Ember.js C#/.net SessionId 标头

问题描述 投票:0回答:1

我有一个 Ember.js SPA,它通过 AXIOS 调用 .net 服务。一切都正常,直到我开始检查 SessionId 标头以在过期时返回 401。

只要令牌有效,我的中间件就会获取会话 ID,调用就会继续,一切都很好。

问题是这样的,当会话id过期时,我的C#/.net中间件返回401。但是AXIOS没有填写error.response.status:response is undefined。

我想要的是将 AXIOS 拦截器的 error.response.status 设置为 401,以便我可以将用户重定向到登录路由。我对 CORS、OPTIONS 和预检请求的理解非常薄弱。我在不同的网站上找到了很多文献,但似乎那里解决的问题只是收到了请求。到目前为止,我还没有找到如何通过 AXIOS 接收响应。

    // Add a response interceptor
    this._axios.interceptors.response.use(
        (response) => {
            return response
        },
        (error) => {
            if (error?.response?.status == 401) { // error is defined, but error.response is undefined.

在 C#/.net 方面

services.AddCors(options =>
{
options.AddPolicy(name: CorsDefaultPolicy,
    policy =>
    {
        policy.WithOrigins("https://localhost:4200", "https://*.MyCompany.io")
            .SetIsOriginAllowedToAllowWildcardSubdomains()
            .SetPreflightMaxAge(TimeSpan.FromHours(1))
            .AllowCredentials()
            .AllowAnyHeader()
            .AllowAnyMethod();
    });
});
javascript c# axios ember.js cors
1个回答
0
投票

我很傻。也许这也会防止其他人变得愚蠢。

我尝试强制中间件返回值。下面的注释行是问题的根源。不要尝试写入响应来获取特定的错误消息文本。只是不要。

        catch (SessionExpiredException e)
        {
            var outputModel = new SuccessOutputModel
            {
                WasSuccessful = false,
                Status = "Session is invalid. Please try to sign-in again.",
            };
            logger.Error(e.Message);
            context.Response.StatusCode = StatusCodes.Status401Unauthorized;
            //context.Response.ContentType = "application/json";
            //await context.Response.WriteAsync(JsonConvert.SerializeObject(outputModel));
        }
© www.soinside.com 2019 - 2024. All rights reserved.