Can't load API from localhost from Angular Client with C# Backend hosted on a standalone unit

huangapple go评论64阅读模式
英文:

Can't load API from localhost from Angular Client with C# Backend hosted on a standalone unit

问题

I am running an Angular Client (v14) with a .Net 6 WebAPI. 运行一个使用 Angular 客户端(版本14)和 .Net 6 WebAPI 的项目。
These are in separate .Net projects running on a Raspberry Pi. 这些项目是在树莓派上运行的不同 .Net 项目中。
It is a standalone kiosk, so the front and backend run on the same box. 这是一个独立的信息亭,前端和后端在同一台设备上运行。

I want to be able to access the front end from a PC on the same network, via the browser. 我希望能够通过浏览器从同一网络上的计算机访问前端。
When I remote to the address of the Raspberry Pi, I can see the loading screen of the Angular App, but it can't resolve localhost, as it looks to the PC's localhost for the backend, not the Kiosk. 当我远程连接到树莓派的地址时,我可以看到 Angular 应用的加载屏幕,但它无法解析 localhost,因为它将后端的请求定向到了PC的 localhost,而不是信息亭的 localhost。

I also want to be able to access the API remotely to control the functions of the unit, this might be via Postman or a third-party application. 但我还希望能够远程访问 API 以控制该单位的功能,可能是通过 Postman 或第三方应用程序。但这可能是一个单独的问题,我以后可以解决。

I need the angular application to rewrite the localhost to the current IP address. 我需要 Angular 应用程序将 localhost 重写为当前的 IP 地址。
I've struggled to find examples of how this is done and things have got quite convoluted along the way. 我苦苦寻找如何实现这一点的示例,而且事情已经变得相当复杂。
Below are sections of the configuration, I wonder if someone can point me in the right direction or point me to an example of how I can make this work? 以下是配置的部分,我想知道是否有人能指导我正确的方向,或者为我提供一个示例,以便我可以使其工作?

launchSettings.json

{
  "profiles": {
    "Kiosk_ClientApp": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://0.0.0.0:4200"
    }
  }
}

nginx

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    include snippets/self-signed.conf;
    include snippets/ssl-params.conf;
    
    server_name kiosk;
    
    location / {
        proxy_pass https://localhost:4200;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_cookie_path / "/; SameSite = None' secure";
        proxy_read_timeout 18000s;
        proxy_send_timeout 18000s;
    }
    
    location /api { 
        proxy_pass https://localhost:4901;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

proxy.conf.json

{
  "/api": {
    "target": "https://0.0.0.0:4901",
    "secure": true,
    "changeOrigin": true,
    "logLevel": "debug"
  }
}

environment.ts

export const environment = {
  production: false,
  urlAddress: "https://localhost:4901"
};

environment.prod.ts

export const environment = {
  production: true,
  urlAddress: "https://localhost:4901"
};

angular.json

{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"Kiosk": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"progress": false,
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/assets"
],
"styles": [
"node_modules/bootstrap/dist/css/bootstrap.min.css",
"src/styles.css",
"node_modules/sweetalert2/src/sweetalert2.scss",
"node_modules/datatables.net-bs4/css/dataTables.bootstrap4.min.css",
"node_modules/datatables.net-dt/css/jquery.dataTables.css"
],
"scripts": [
"./node_modules/jquery/dist/jquery.min.js",
"./node_modules/popper.js/dist/umd/popper.min.js",
"./node_modules/bootstrap/dist/js/bootstrap.min.js",
"./node_modules/chart.js/dist/Chart.js",
"node_modules/jquery/dist/jquery.js",
"node_modules/datatables.net/js/jquery.dataTables.js",
"node_modules/datatables.net-bs4/js/dataTables.bootstrap4.min.js"
],
"vendorChunk": true,
"extractLicenses": false,
"buildOptimizer": false,
"sourceMap": true,
"optimization": false,
"namedChunks": true
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "anyComponentStyle",
"maximumWarning": "6kb"
}
]
}
},
"defaultConfiguration": ""
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "Kiosk:build",
"proxyConfig": "src/proxy.conf.json",
"ssl": true,
"sslCert": "ssl/server.crt",
"sslKey": "ssl/server.key"
<details>
<summary>英文:</summary>
I am running an Angular Client (v14) with a .Net 6 WebAPI.  These are in separate .Net projects running on a Raspberry Pi.  It is a standalone kiosk,  so the front and backend run on the same box.
I want to be able to access the front end from a PC on the same network, via the browser.  When I remote to the address of the Raspberry Pi, I can see the loading screen of the Angular App, but it can&#39;t resolve localhost, as it looks to the PC&#39;s localhost for the backend, not the Kiosk.
I also want to be able to access the API remotely to control the functions of the unit, this might be via Postman or a third party application.  But this maybe a separate issue, which I can solve later.
I need the angular application to rewrite the localhost to the current IP address.  I&#39;ve struggled to find example of how this is done and things have got quite convoluted along the way.  Below are sections of the configuration, I wonder if someone can point me in the right direction or point me to an example of how I can make this work? 
launchSettings.json
{
&quot;profiles&quot;: {
&quot;Kiosk_ClientApp&quot;: {
&quot;commandName&quot;: &quot;Project&quot;,
&quot;environmentVariables&quot;: {
&quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Development&quot;
},
&quot;applicationUrl&quot;: &quot;https://0.0.0.0:4200&quot;
}
}
}
nginx
server {
listen 443 ssl;
listen [::]:443 ssl;
include snippets/self-signed.conf;
include snippets/ssl-params.conf;
server_name kiosk;
location / {
proxy_pass https://localhost:4200;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_cookie_path / &quot;/; SameSite = None&#39; secure&quot;;
proxy_read_timeout 18000s;
proxy_send_timeout 18000s;
}
location /api { 
proxy_pass https://localhost:4901;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection &#39;upgrade&#39;;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
proxy.conf.json
{
&quot;/api&quot;: {
&quot;target&quot;: &quot;https://0.0.0.0:4901&quot;,
&quot;secure&quot;: true,
&quot;changeOrigin&quot;: true,
&quot;logLevel&quot;: &quot;debug&quot;
}
}
environment.ts
export const environment = {
production: false,
urlAddress: &quot;https://localhost:4901&quot;
};
environment.prod.ts
export const environment = {
production: true,
urlAddress: &quot;https://localhost:4901&quot;
};
angular.json
{
&quot;$schema&quot;: &quot;./node_modules/@angular/cli/lib/config/schema.json&quot;,
&quot;version&quot;: 1,
&quot;newProjectRoot&quot;: &quot;projects&quot;,
&quot;projects&quot;: {
&quot;Kiosk&quot;: {
&quot;root&quot;: &quot;&quot;,
&quot;sourceRoot&quot;: &quot;src&quot;,
&quot;projectType&quot;: &quot;application&quot;,
&quot;prefix&quot;: &quot;app&quot;,
&quot;schematics&quot;: {},
&quot;architect&quot;: {
&quot;build&quot;: {
&quot;builder&quot;: &quot;@angular-devkit/build-angular:browser&quot;,
&quot;options&quot;: {
&quot;progress&quot;: false,
&quot;outputPath&quot;: &quot;dist&quot;,
&quot;index&quot;: &quot;src/index.html&quot;,
&quot;main&quot;: &quot;src/main.ts&quot;,
&quot;polyfills&quot;: &quot;src/polyfills.ts&quot;,
&quot;tsConfig&quot;: &quot;src/tsconfig.app.json&quot;,
&quot;assets&quot;: [
&quot;src/assets&quot;
],
&quot;styles&quot;: [
&quot;node_modules/bootstrap/dist/css/bootstrap.min.css&quot;,
&quot;src/styles.css&quot;,
&quot;node_modules/sweetalert2/src/sweetalert2.scss&quot;,
&quot;node_modules/datatables.net-bs4/css/dataTables.bootstrap4.min.css&quot;,
&quot;node_modules/datatables.net-dt/css/jquery.dataTables.css&quot;
],
&quot;scripts&quot;: [
&quot;./node_modules/jquery/dist/jquery.min.js&quot;,
&quot;./node_modules/popper.js/dist/umd/popper.min.js&quot;,
&quot;./node_modules/bootstrap/dist/js/bootstrap.min.js&quot;,
&quot;./node_modules/chart.js/dist/Chart.js&quot;,
&quot;node_modules/jquery/dist/jquery.js&quot;,
&quot;node_modules/datatables.net/js/jquery.dataTables.js&quot;,
&quot;node_modules/datatables.net-bs4/js/dataTables.bootstrap4.min.js&quot;
],
&quot;vendorChunk&quot;: true,
&quot;extractLicenses&quot;: false,
&quot;buildOptimizer&quot;: false,
&quot;sourceMap&quot;: true,
&quot;optimization&quot;: false,
&quot;namedChunks&quot;: true
},
&quot;configurations&quot;: {
&quot;production&quot;: {
&quot;fileReplacements&quot;: [
{
&quot;replace&quot;: &quot;src/environments/environment.ts&quot;,
&quot;with&quot;: &quot;src/environments/environment.prod.ts&quot;
}
],
&quot;optimization&quot;: true,
&quot;outputHashing&quot;: &quot;all&quot;,
&quot;sourceMap&quot;: false,
&quot;namedChunks&quot;: false,
&quot;extractLicenses&quot;: true,
&quot;vendorChunk&quot;: false,
&quot;buildOptimizer&quot;: true,
&quot;budgets&quot;: [
{
&quot;type&quot;: &quot;anyComponentStyle&quot;,
&quot;maximumWarning&quot;: &quot;6kb&quot;
}
]
}
},
&quot;defaultConfiguration&quot;: &quot;&quot;
},
&quot;serve&quot;: {
&quot;builder&quot;: &quot;@angular-devkit/build-angular:dev-server&quot;,
&quot;options&quot;: {
&quot;browserTarget&quot;: &quot;Kiosk:build&quot;,
&quot;proxyConfig&quot;: &quot;src/proxy.conf.json&quot;,
&quot;ssl&quot;: true,
&quot;sslCert&quot;: &quot;ssl/server.crt&quot;,
&quot;sslKey&quot;: &quot;ssl/server.key&quot;
},
&quot;configurations&quot;: {
&quot;production&quot;: {
&quot;browserTarget&quot;: &quot;Kiosk:build:production&quot;
}
}
},
&quot;extract-i18n&quot;: {
&quot;builder&quot;: &quot;@angular-devkit/build-angular:extract-i18n&quot;,
&quot;options&quot;: {
&quot;browserTarget&quot;: &quot;Kiosk:build&quot;
}
},
&quot;test&quot;: {
&quot;builder&quot;: &quot;@angular-devkit/build-angular:karma&quot;,
&quot;options&quot;: {
&quot;main&quot;: &quot;src/test.ts&quot;,
&quot;polyfills&quot;: &quot;src/polyfills.ts&quot;,
&quot;tsConfig&quot;: &quot;src/tsconfig.spec.json&quot;,
&quot;karmaConfig&quot;: &quot;src/karma.conf.js&quot;,
&quot;styles&quot;: [
&quot;src/styles.css&quot;
],
&quot;scripts&quot;: [],
&quot;assets&quot;: [
&quot;src/assets&quot;
]
}
},
&quot;server&quot;: {
&quot;builder&quot;: &quot;@angular-devkit/build-angular:server&quot;,
&quot;options&quot;: {
&quot;outputPath&quot;: &quot;dist-server&quot;,
&quot;main&quot;: &quot;src/main.ts&quot;,
&quot;tsConfig&quot;: &quot;src/tsconfig.server.json&quot;,
&quot;sourceMap&quot;: true,
&quot;optimization&quot;: false
},
&quot;configurations&quot;: {
&quot;dev&quot;: {
&quot;optimization&quot;: true,
&quot;outputHashing&quot;: &quot;all&quot;,
&quot;sourceMap&quot;: false,
&quot;namedChunks&quot;: false,
&quot;extractLicenses&quot;: true
},
&quot;production&quot;: {
&quot;optimization&quot;: true,
&quot;outputHashing&quot;: &quot;all&quot;,
&quot;sourceMap&quot;: false,
&quot;namedChunks&quot;: false,
&quot;extractLicenses&quot;: true
}
},
&quot;defaultConfiguration&quot;: &quot;&quot;
}
}
},
&quot;Kiosk-e2e&quot;: {
&quot;root&quot;: &quot;e2e/&quot;,
&quot;projectType&quot;: &quot;application&quot;,
&quot;architect&quot;: {
&quot;e2e&quot;: {
&quot;builder&quot;: &quot;@angular-devkit/build-angular:protractor&quot;,
&quot;options&quot;: {
&quot;protractorConfig&quot;: &quot;e2e/protractor.conf.js&quot;,
&quot;devServerTarget&quot;: &quot;Kiosk:serve&quot;
}
}
}
}
},
&quot;cli&quot;: {
&quot;analytics&quot;: false
}
}
Example Post:
saveRecipe(recipe: IRecipe): Observable&lt;number&gt; {
this.convertRecipeToMetric(recipe);
return this.http
.post&lt;number&gt;(this.createCompleteRoute(&quot;api/Recipe/AddRecipe&quot;, this.envUrl.urlAddress),
recipe,
{ withCredentials: true }).pipe(map((s: number) =&gt; { return s; })
);
}
private createCompleteRoute = (route: string, envAddress: string) =&gt; {
return `${envAddress}/${route}`;
};
package.json - script section:
&quot;scripts&quot;: {
&quot;ng&quot;: &quot;ng&quot;,
&quot;start&quot;: &quot;ng serve --proxy-config proxy.config.json&quot;,
&quot;build&quot;: &quot;ng build&quot;,
&quot;build:ssr&quot;: &quot;ng run Kiosk:server:dev&quot;,
&quot;test&quot;: &quot;ng test&quot;,
&quot;lint&quot;: &quot;ng lint&quot;,
&quot;e2e&quot;: &quot;ng e2e&quot;
},
C# Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =&gt;
{
options.AddPolicy(
&quot;AllowMyOrigins&quot;,
builder =&gt;
builder
.AllowCredentials()
.WithOrigins(&quot;https://localhost:4200&quot;)
.SetIsOriginAllowed(host =&gt; true)
.SetIsOriginAllowedToAllowWildcardSubdomains()
.AllowAnyHeader()
.AllowAnyMethod());
});
services.AddControllersWithViews();
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =&gt; { configuration.RootPath = &quot;ClientApp/dist&quot;; });
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(&quot;/Error&quot;);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto,
});
app.UseHttpsRedirection();
app.UseStaticFiles();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}
app.UseRouting();
app.UseCors(&quot;AllowMyOrigins&quot;);
app.UseEndpoints(endpoints =&gt;
{
endpoints.MapControllerRoute(
&quot;default&quot;,
&quot;{controller}/{action=Index}/{id?}&quot;);
});
app.UseSpa(spa =&gt;
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = &quot;ClientApp&quot;;
if (env.IsDevelopment())
{
spa.UseAngularCliServer(&quot;start&quot;);
}
});
}
The IP address could be anything as there would be many of these units, placed anywhere, so I need the IP address dynamic, not static.
</details>
# 答案1
**得分**: 1
如果您正在使用nginx作为入口代理,则前端代码中不应使用绝对路径。
如果您使用相对路径从JavaScript中调用API(即"/api/something"),则浏览器将访问您用于加载前端的服务器(可能是一个访问nginx代理的服务器URL)。
<details>
<summary>英文:</summary>
If you&#39;re using nginx as an ingress proxy then you shouldn&#39;t use absolute paths in your front-end code.
If you use relative paths to call the API (i.e. &quot;/api/something&quot;) from javascript then the browser will hit whatever server you&#39;re using to load the FE (which presumably is a server URL that hits the nginx proxy?)
</details>
# 答案2
**得分**: -1
问题似乎是Angular客户端无法向运行在本地主机上的单独C#后端API发出请求。这可能是由于CORS(跨域资源共享)限制引起的,这些限制阻止网页向不同于提供页面的域的域发出请求。
要解决此问题,您需要配置C#后端以允许来自Angular客户端域的跨域请求。一种方法是通过向C#后端添加CORS中间件来实现这一点。以下是如何使用Microsoft.AspNetCore.Cors包执行此操作的示例:
1. 使用NuGet安装Microsoft.AspNetCore.Cors包。
2. 在C#后端的Startup.cs文件中,在ConfigureServices方法中添加以下代码以配置CORS:
```csharp
services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});

这将允许任何来源发出请求到您的API,并允许任何HTTP方法和标头。

  1. 在Startup.cs的Configure方法中,添加以下代码以启用CORS:
app.UseCors();

这将为应用程序中的所有端点启用CORS。

通过这些更改,您的C#后端现在应该允许来自Angular客户端域的请求。请注意,应谨慎允许任何来源发出请求到您的API,因为这可能存在安全风险。通常建议限制允许发出请求到您的API的域。

英文:

The issue appears to be that the Angular client is unable to make requests to an API hosted on a separate C# backend that is running on localhost. This is likely due to CORS restrictions, which prevent web pages from making requests to a different domain than the one that served the page.

To fix this issue, you will need to configure the C# backend to allow cross-origin requests from the Angular client's domain. One way to do this is by adding CORS middleware to the C# backend. Here's an example of how to do this using the Microsoft.AspNetCore.Cors package:

Install the Microsoft.AspNetCore.Cors package using NuGet.
In your C# backend's Startup.cs file, add the following code to the ConfigureServices method to configure CORS:

services.AddCors(options =&gt;
{
options.AddDefaultPolicy(builder =&gt;
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});

This will allow any origin to make requests to your API and allow any HTTP method and headers.

In the Configure method of Startup.cs, add the following code to enable CORS:

app.UseCors();

This will enable CORS for all endpoints in your application.

With these changes, your C# backend should now allow requests from the Angular client's domain. Note that you should be cautious about allowing any origin to make requests to your API, as this can pose a security risk. It's generally a good idea to limit the origins that are allowed to make requests to your API.

huangapple
  • 本文由 发表于 2023年4月6日 22:57:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定