【问题标题】:Asp.net Core authorization fails only for xamarin forms application仅 xamarin 表单应用程序的 Asp.net Core 授权失败
【发布时间】:2020-06-14 02:48:19
【问题描述】:

我开发了一个简单的应用程序来练习。 该组合是带有 JWT Token 的 ASP.NET CORE。

在我的 asp.net core 3.1 服务器上,我有这个 DI 代码:

services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options =>
                {
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = true,
                        ValidateAudience = true,
                        ValidateLifetime = true,
                        ValidateIssuerSigningKey = true,
                        ValidIssuer = configuration["jwt:Issuer"],
                        ValidAudience = configuration["jwt:Audience"],
                        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["jwt:Key"]))
                    };
                });

我可以轻松地与 Postman 或我的 BlazorApp 连接以生成令牌并使用该令牌使用其他端点。 在 Blazor 上,我只需要插入一个 DefaultHeader:

_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);

现在,我正在尝试对 Xamarin Forms 应用程序执行相同的操作。 我尝试在默认标头上使用生成的令牌,但收到未经授权的响应。

要调试 Xamarin Forms 应用程序,我必须将我的 localhost 端点名称更改为 127.0.0.1(以便模拟器可以访问我机器上的 asp.net API)。

我尝试将我的 JWT 令牌的颁发者更改为 127.0.0.1 而不是 localhost 但没有成功。

有人可以帮忙吗?

提前致谢。

【问题讨论】:

标签: xamarin.forms jwt asp.net-core-webapi dotnet-httpclient blazor-webassembly


【解决方案1】:

两天后我收到了。

我的 asp.net 核心 api 使用的是 HttpsRedirect,而我的 Xamarin Mobile 没有指向 HTTPS 端口。

我不知道为什么,因为 postman 和 blazor 可以同时使用两个端点(HTTP 和 https 端口),但是 Xamarin 自从我使用 https 端点后就开始工作了。

现在好了。 还是谢谢

【讨论】:

    猜你喜欢
    • 2018-08-25
    • 2018-05-20
    • 2021-06-02
    • 1970-01-01
    • 2019-05-19
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多