我有一个小型演示应用程序:
从FE到BE的get请求似乎工作正常。 但是POST请求抛出:
跨域请求被阻止:“同源起源”策略禁止读取https://localhost:44333/api/auth/login处的远程资源。 (原因:CORS请求未成功)
HTTP正常工作。唯一的问题是HTTPS。
Angular服务如下:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
@Injectable()
export class AuthService {
baseUrl = 'https://localhost:44333/api/auth/';
constructor(private http: HttpClient) {
}
login(model: any) {
return this.http.post(this.baseUrl + 'login', model)
.pipe(
map((response: any) => {
const user = response;
if (user) {
localStorage.setItem('token', user.token);
}
}));
}
}
现在在BE方面,允许任何起源:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// 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.UseHttpsRedirection();
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.UseAuthentication();
app.UseMvc();
}
上可用的应用程序来源
dotnet服务器控制台中的错误:
Connection id "0HLK56HC2M58N" request processing ended abnormally. System.IO.IOException: The decryption operation failed,
请参阅内部异常。 ---> System.ComponentModel.Win32Exception:一个 处理证书时发生未知错误---结束 内部异常堆栈跟踪--- System.Net.Security.SslStreamInternal.ReadAsyncInternal [TReadAdapter](TReadAdapter 适配器,内存
1 buffer) at Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal.AdaptedPipeline.ReadInputAsync(Stream stream) at System.IO.Pipelines.PipeCompletion.ThrowLatchedException() at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result) at System.IO.Pipelines.Pipe.ReadAsync(CancellationToken token) at System.IO.Pipelines.Pipe.DefaultPipeReader.ReadAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.BeginRead(ValueTask
1& 等待) Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests [TContext](IHttpApplication1 application) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication
1 应用程序)信息:Microsoft.AspNetCore.Server.Kestrel [20] 连接标识“ 0HLK56HC2M58O”的请求处理异常结束。 System.IO.IOException:解密操作失败, 看到内部异常。 ---> System.ComponentModel.Win32Exception:一个 处理证书时发生未知错误---结束 内部异常堆栈跟踪--- System.Net.Security.SslStreamInternal.ReadAsyncInternal [TReadAdapter](TReadAdapter 适配器,内存1 buffer) at Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal.AdaptedPipeline.ReadInputAsync(Stream stream) at System.IO.Pipelines.PipeCompletion.ThrowLatchedException() at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result) at System.IO.Pipelines.Pipe.ReadAsync(CancellationToken token) at System.IO.Pipelines.Pipe.DefaultPipeReader.ReadAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.BeginRead(ValueTask
1& 等待) Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests [TContext](IHttpApplication1 application) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication
1 应用程序)信息:Microsoft.AspNetCore.Server.Kestrel [20] 连接标识“ 0HLK56HC2M58Q”的请求处理异常结束。 System.IO.IOException:解密操作失败, 看到内部异常。 ---> System.ComponentModel.Win32Exception:一个 处理证书时发生未知错误---结束 内部异常堆栈跟踪--- System.Net.Security.SslStreamInternal.ReadAsyncInternal [TReadAdapter](TReadAdapter 适配器,内存1 buffer) at Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal.AdaptedPipeline.ReadInputAsync(Stream stream) at System.IO.Pipelines.PipeCompletion.ThrowLatchedException() at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result) at System.IO.Pipelines.Pipe.ReadAsync(CancellationToken token) at System.IO.Pipelines.Pipe.DefaultPipeReader.ReadAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.BeginRead(ValueTask
1& 等待) Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests [TContext](IHttpApplication1 application) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication
1 应用程序)信息:Microsoft.AspNetCore.Server.Kestrel [20] 连接标识“ 0HLK56HC2M58P”的请求处理异常结束。 System.IO.IOException:解密操作失败, 看到内部异常。 ---> System.ComponentModel.Win32Exception:一个 处理证书时发生未知错误---结束 内部异常堆栈跟踪--- System.Net.Security.SslStreamInternal.ReadAsyncInternal [TReadAdapter](TReadAdapter 适配器,内存1 buffer) at Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal.AdaptedPipeline.ReadInputAsync(Stream stream) at System.IO.Pipelines.PipeCompletion.ThrowLatchedException() at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result) at System.IO.Pipelines.Pipe.GetReadAsyncResult() at System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token)
1 申请) Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync [TContext](IHttpApplication`1 应用程序)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication
答案 0 :(得分:0)
似乎您需要添加AllowCredentials方法。
请查看此documentation,了解跨域请求中的凭据。
app.UseHttpsRedirection();
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
app.UseAuthentication();
app.UseMvc();
答案 1 :(得分:0)
在开发时允许使用通配符的CORS。
//Allow CORS, * wildcard only for development period, be specific once done
app.UseCors(builder => builder.WithOrigins("http://localhost:4200")
.AllowAnyHeader().AllowAnyMethod().AllowCredentials());
希望这会有所帮助:)