我完全堅持實施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請求的一個例子。我並不是完全遵循你的解釋,所以我認爲這可能會幫助我或其他人爲這次旅程而來。 –