TypeError:无法使用Edge和Blazor进行提取



我有一个WPA Blazor应用程序,它的后端使用ASP.NET MVC API。

我有一个文件导入和上传过程,可以运行相当长的时间(20多分钟(。我已经将HTTPContext设置为30分钟的超时(在Razor页面上(。但在大约4-5分钟的过程中,我收到了以下错误(文本来自Microsoft Edge Developers Tools Console(:

在'获取的访问权限https://myapp.azurewebsites.net/fileupload?aimporttype=InsertProduction&ausername='来自原点'https://myapp.azurewebsites.net'已被CORS策略阻止:请求的资源上不存在"Access Control Allow Origin"标头。如果不透明响应满足您的需求,请将请求的模式设置为"无cors",以在禁用cors的情况下获取资源。

我认为这在以下代码中解决了这个问题:

这是API的启动类:

public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed((host) => true)
.AllowAnyHeader());
});
services.AddDbContext<ProductionContext>(opt => opt.UseSqlServer(AppSettings.Instance.GetConnection("SQLAZURE_Connection")));

services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(options =>
{
Configuration.Bind("AzureAd", options);
options.TokenValidationParameters.RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
},
options => { Configuration.Bind("AzureAd", options); });
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ProductionAPI", Version = "v1" });
});
services.AddControllers();
AppSettings.Instance.SetConfiguration(Configuration);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment() || env.IsProduction())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "ProductionAPI v1");
c.RoutePrefix = "";     // Shows the SwaggerUI page
});
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("CorsPolicy");
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}

以及我从客户端进行的SubmitFileAsync调用:

public async Task SubmitFileAsync()
{
try
{
if (string.IsNullOrEmpty(_selectionValue))
{
_importSelectionCheckVisible = true;
return;
}
_uploadResult = "Uploading file...";
var lContent = new MultipartFormDataContent();
lContent.Add(
content: _fileStream,
name: ""file"",
fileName: _selectedFileName);
string lUri = settings.ApiUrl + $"fileupload?aimporttype={_selectionValue}&ausername={loginState.Username}";
HttpResponseMessage lResponse = await http.PostAsync(lUri, lContent);
if (lResponse.IsSuccessStatusCode)
{
var lJsonString = lResponse.Content.ReadAsStringAsync();
ImportResults AImportResults =
System.Text.Json.JsonSerializer.Deserialize<ImportResults>(lJsonString.Result);
_importVisibility = "visibility: visible";
_importRowCount = AImportResults.ImportRowCount.ToString();
_totalRowsAdded = AImportResults.TotalRowsAdded.ToString();
_totalRowsSkipped = AImportResults.TotalRowsSkipped.ToString();
_carriersCreated = AImportResults.CarriersCreated.ToString();
_insuranceTypesCreated = AImportResults.InsuranceTypesCreated.ToString();
_transactionTypesCreated = AImportResults.TransactionTypesCreated.ToString();
_totalRecordsImported = AImportResults.TotalRecordsImported.ToString();
_importSuccess = AImportResults.Success;
_duplicateRecords = AImportResults.DuplicatesDetected.ToString();
_uploadResult = "Upload Complete";
StateHasChanged();
}
else
{
_uploadResult = $"Invalid file type and/or format. ";
}
}
catch (Exception ex)
{
_uploadResult = "Select a CSV file to upload.";
_error = ex.Message;
}
}

我在这里错过了什么?

因此,事实证明这实际上是一个授权令牌超时问题,而不是COR的问题。

最新更新