Blazor无法连接到ASP.NET Core WebApi(CROS)

用户名

我有一个https://192.168.188.31:44302使用Web API Enpoints在本地IP运行的ASP.NET Core Server 我可以使用VS Code REST Client连接到所述服务器。现在,我想在Blazor WebAssembly上运行Web API https://192.168.188.31:5555

我的Blozor代码:

@page "/login"
@inject HttpClient Http

[ ... some "HTML"-Code ... ]

@code {
    private async Task Authenticate()
    {
        var loginModel = new LoginModel
        {
            Mail = "[email protected]",
            Password = "s3cr3T"
        };
        var requestMessage = new HttpRequestMessage()
        {
            Method = new HttpMethod("POST"),
            RequestUri = ClientB.Classes.Uris.AuthenticateUser(),
            Content =
                JsonContent.Create(loginModel)
        };

        var response = await Http.SendAsync(requestMessage);
        var responseStatusCode = response.StatusCode;

        var responseBody = await response.Content.ReadAsStringAsync();

        Console.WriteLine("responseBody: " + responseBody);
    }

    public async void LoginSubmit(EditContext editContext)
    {
        await Authenticate();
        Console.WriteLine("Debug: Valid Submit");
    }
}

现在触发时,LoginSubmit我在Chrome和Firefox的开发人员控制台中收到以下错误消息:login:1 Access to fetch at 'https://192.168.188.31:44302/user/authenticate' from origin 'https://192.168.188.31:5555' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

我是Web开发的新手,发现您必须在服务器端ASP.NET Core项目上启用CORS,因此我扩展startup.cs

readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<UserDataContext, UserSqliteDataContext>();

services.AddCors(options =>
{
    options.AddPolicy(name: MyAllowSpecificOrigins,
        builder =>
        {
            builder.WithOrigins("https://192.168.188.31:44302",
                "https://192.168.188.31:5555",
                "https://localhost:44302", 
                "https://localhost:5555")
            .AllowAnyHeader()
            .AllowAnyMethod();
        });
});

services.AddControllers();
services.AddApiVersioning(x =>
{
...
});

services.AddAuthentication(x =>
    ...
});
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

services.AddScoped<IViewerService, ViewerService>();
}

public void Configure(IApplicationBuilder app,
    IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    Program.IsDevelopment = env.IsDevelopment();

    app.UseHttpsRedirection();
    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();
    app.UseCors(MyAllowSpecificOrigins);

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });

    Log.Initialize();
}

但是我仍然收到以上错误消息。我在配置CORS时做错了什么?为什么在VS Code REST Client上可以正常使用?在Blazor WASM应用程序中我怎么打错电话?

用户名

导致错误消息的问题login:1 Access to fetch at 'https://192.168.188.31:44302/user/authenticate' from origin 'https://192.168.188.31:5555' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.是由引起的HttpsRedirection

要解决此问题,请HttpsRedirection通过删除app.UseHttpsRedirection();功能中的行来禁用它Configure或者添加适当的端口以用于功能中的重定向ConfigureServices(推荐方式)。

就我而言,我在port处启动WebAPI 44302,因此我的解决方案如下所示(您必须将其调整为您的端口号):

if (Program.IsDevelopment)
{
    services.AddHttpsRedirection(options =>
    {
        options.RedirectStatusCode = StatusCodes.Status308PermanentRedirect;
        options.HttpsPort = 44302;
    });
}
else
{
    services.AddHttpsRedirection(options =>
    {
        options.RedirectStatusCode = StatusCodes.Status308PermanentRedirect;
        options.HttpsPort = 443;
    });
}

还要注意,将请求API的IP地址添加到CORS就足够了,如下所示:

services.AddCors(options =>
{
    options.AddPolicy(name: specificOrigins,
        builder =>
        {
            builder.WithOrigins("https://192.168.188.31:5555",
                "http://192.168.188.31:5444")
            .AllowAnyHeader()
            .AllowAnyMethod();
        });
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法从 javascript 连接到 ASP.NET Core websocket

来自分类Dev

无法将具有EFCore Mysql提供程序的Docker中的Asp.net Core WebApi连接到主机服务器上的非Docker Mysql

来自分类Dev

ReactSample和AsP.NET Core WebApi之间的http连接

来自分类Dev

Appveyoyr 无法构建 ASP.NET Core WebApi 项目

来自分类Dev

Dockerized Asp.net Core无法连接到WSL中的主机的Kafka

来自分类Dev

无法从Docker中的ASP.NET Core 3.1连接到内部SQL Server

来自分类Dev

从IIS(ASP.Net Core)连接到SQL Server

来自分类Dev

无法连接到.NET Core 3.1中的远程SignalR集线器

来自分类Dev

将Firebird连接到ASP.net WebAPI项目

来自分类Dev

ASP.NET Core Web API服务无法连接到同一网络上的Docker SQL Server

来自分类Dev

asp.net core 1.0在webapi中获取Windows身份

来自分类Dev

IFormFile在Asp.Net Core WebAPI中始终为空

来自分类Dev

在ASP.NET Core WebAPI中获取OData计数

来自分类Dev

Asp.Net Core WebApi:创建单元测试

来自分类Dev

从 Ionic 访问 ASP.NET Core WebApi 时出错

来自分类Dev

ASP.NET Core WebAPI:内存缓存与响应缓存

来自分类Dev

asp.net core webapi中的一对多关系

来自分类Dev

Asp.net无法连接到MSSQL

来自分类Dev

无法将自定义设置文件添加到Asp.Net Core 3.1 WebApi

来自分类Dev

Swagger UI 无法显示来自 ASP.net Core WebAPI 的正确响应

来自分类Dev

ASP.NET Core 2.0 webapi 应用程序无法在 Ubuntu 16.04 上运行

来自分类Dev

SignalR .NET客户端连接到Blazor .NET Core 3应用程序中的Azure SignalR服务

来自分类Dev

将LAN上的设备连接到ASP.NET Core Web API

来自分类Dev

Azure上的.NET Core无法连接到SQL Server数据库

来自分类Dev

无法通过Web API .NET Core连接到SQL Server Docker容器

来自分类Dev

.NET Core WebAPI VueJS模板发布问题

来自分类Dev

.NET Core 3.1 WebAPI IISExpress挂起调试

来自分类Dev

.NET Core WebAPI 永久令牌认证

来自分类Dev

在 .net core webAPI 中启用 CORS

Related 相关文章

  1. 1

    无法从 javascript 连接到 ASP.NET Core websocket

  2. 2

    无法将具有EFCore Mysql提供程序的Docker中的Asp.net Core WebApi连接到主机服务器上的非Docker Mysql

  3. 3

    ReactSample和AsP.NET Core WebApi之间的http连接

  4. 4

    Appveyoyr 无法构建 ASP.NET Core WebApi 项目

  5. 5

    Dockerized Asp.net Core无法连接到WSL中的主机的Kafka

  6. 6

    无法从Docker中的ASP.NET Core 3.1连接到内部SQL Server

  7. 7

    从IIS(ASP.Net Core)连接到SQL Server

  8. 8

    无法连接到.NET Core 3.1中的远程SignalR集线器

  9. 9

    将Firebird连接到ASP.net WebAPI项目

  10. 10

    ASP.NET Core Web API服务无法连接到同一网络上的Docker SQL Server

  11. 11

    asp.net core 1.0在webapi中获取Windows身份

  12. 12

    IFormFile在Asp.Net Core WebAPI中始终为空

  13. 13

    在ASP.NET Core WebAPI中获取OData计数

  14. 14

    Asp.Net Core WebApi:创建单元测试

  15. 15

    从 Ionic 访问 ASP.NET Core WebApi 时出错

  16. 16

    ASP.NET Core WebAPI:内存缓存与响应缓存

  17. 17

    asp.net core webapi中的一对多关系

  18. 18

    Asp.net无法连接到MSSQL

  19. 19

    无法将自定义设置文件添加到Asp.Net Core 3.1 WebApi

  20. 20

    Swagger UI 无法显示来自 ASP.net Core WebAPI 的正确响应

  21. 21

    ASP.NET Core 2.0 webapi 应用程序无法在 Ubuntu 16.04 上运行

  22. 22

    SignalR .NET客户端连接到Blazor .NET Core 3应用程序中的Azure SignalR服务

  23. 23

    将LAN上的设备连接到ASP.NET Core Web API

  24. 24

    Azure上的.NET Core无法连接到SQL Server数据库

  25. 25

    无法通过Web API .NET Core连接到SQL Server Docker容器

  26. 26

    .NET Core WebAPI VueJS模板发布问题

  27. 27

    .NET Core 3.1 WebAPI IISExpress挂起调试

  28. 28

    .NET Core WebAPI 永久令牌认证

  29. 29

    在 .net core webAPI 中启用 CORS

热门标签

归档