我完全坚持实施Windows身份验证为我的.NET Core应用程序之一,使用Aurelia的客户端。Aurelia Windows身份验证 - 后401未授权
Aurelia酒店的应用程序托管在端口:9000和.NET的WebAPI托管在端口:9001。
这个想法是一旦应用程序发布,但现在在开发中,我的.NET应用程序提供静态页面,我使用端口:9000,因为Aurelia提供的BrowserSync。
当我使用端口:9000这一切都很好,并且我没有问题张贴或获取。
如果我切换到端口:9001我仍然可以但不能发布。在401 Unauthorized
中发布结果。
如果我们看一下标题为端口:9000请求..
获取(成功):
邮报(失败):
你可以看到,有多个消息头,后因某些原因,最重要的是身份验证Cookie丢失..
基Repo.js
import {inject} from 'aurelia-framework';
import {HttpClient, json} from 'aurelia-fetch-client';
import {AppSettings} from '../infrastructure/app-settings';
@inject(HttpClient, AppSettings)
export class BaseRepo {
constructor(http, appSettings) {
http.configure(config => {
config
.withDefaults({
credentials: 'include',
headers: {
'Accept': 'application/json'
}
})
.withInterceptor({
request(request) {
console.log(`Requesting ${request.method} ${request.url}`);
return request;
},
response(response) {
console.log(`Received ${response.status} ${response.url}`);
return response;
}
})
});
this.http = http;
this.baseUrl = appSettings.api;
}
get(url) {
console.log('BaseRepo(get): ' + url);
return this.http.fetch(this.baseUrl + url)
.then(response => { return response.json(); })
.then(data => { return data; });
}
post(url, data) {
console.log('BaseRepo(post): ' + url, data);
return this.http.fetch(this.baseUrl + url, {
method: 'post',
body: json(data)
})
.then(response => response.json())
.then(data => { return data; });
}
}
为什么获得工作而不是POST当使用BrowserSync端口?
编辑1
后(成功)端口:9001:
编辑2 控制台消息交错误:
OPTIONS
http://localhost:9001/api/MYURLS 401 (Unauthorized)
Fetch API cannot load http://localhost:9001/api/MYURLS . Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:9000 ' is therefore not allowed access. The response had HTTP status code 401. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
编辑3
Startup.cs
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
env.ConfigureNLog("nlog.config");
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
services.AddMemoryCache();
services.AddMvc();
services.InjectWebServices();
services.AddOptions();
//call this in case you need aspnet-user-authtype/aspnet-user-identity
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IConfiguration>(Configuration);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseCors("CorsPolicy");
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
app.UseDefaultFiles();
app.UseStaticFiles();
//add NLog to ASP.NET Core
loggerFactory.AddNLog();
//add NLog.Web
app.AddNLogWeb();
}
}
您可以显示与您的文章相同的截图,但是对于端口9000?我不明白如果您点击端口9000,您将如何获得端口9001托管的Web API的响应。 –
对不起!我的意思是端口9000.截图是端口9000.我将编辑我的问题。你想让我发布9001端口截图吗? – Reft
我希望看到您的工作POST请求的一个例子。我并不是完全遵循你的解释,所以我认为这可能会帮助我或其他人为这次旅程而来。 –