2017-08-15 79 views
3

我完全堅持實施Windows身份驗證爲我的.NET Core應用程序之一,使用Aurelia的客戶端。Aurelia Windows身份驗證 - 後401未授權

Aurelia酒店的應用程序託管在端口:9000和.NET的WebAPI託管在端口:9001

這個想法是一旦應用程序發佈,但現在在開發中,我的.NET應用程序提供靜態頁面,我使用端口:9000,因爲Aurelia提供的BrowserSync。

當我使用端口:9000這一切都很好,並且我沒有問題張貼或獲取。

如果我切換到端口:9001我仍然可以但不能發佈。在401 Unauthorized中發佈結果。

如果我們看一下標題爲端口:9000請求..

獲取(成功):

enter image description here

郵報(失敗):

enter image description here

你可以看到,有多個消息頭,後因某些原因,最重要的是身份驗證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:

enter image description here

編輯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(); 
     } 
    } 
+0

您可以顯示與您的文章相同的截圖,但是對於端口9000?我不明白如果您點擊端口9000,您將如何獲得端口9001託管的Web API的響應。 –

+0

對不起!我的意思是端口9000.截圖是端口9000.我將編輯我的問題。你想讓我發佈9001端口截圖嗎? – Reft

+0

我希望看到您的工作POST請求的一個例子。我並不是完全遵循你的解釋,所以我認爲這可能會幫助我或其他人爲這次旅程而來。 –

回答

1

enter image description here

我啓用了項目屬性「啓用匿名身份驗證」,瞧...

我只用了「啓用Windows Authenticaiton」啓用之前,現在這兩個端口的工作!

當應用程序部署時,無論如何不會啓用,因爲那時我將使用真正的IIS。

更新1

升級到.NET 2.0的核心後,我不再能夠啓用Windows身份驗證和匿名身份驗證。 經過一番研究,我發現你必須添加:

services.AddAuthentication(IISDefaults.AuthenticationScheme); 
在startup.cs

,以便爲它工作。

更多信息可在comment部分和docs找到。

更新2

你需要Microsoft.AspNetCore.Authentication包進行驗證建設者。

+1

Whew。我很高興這一切都結束了...... –

1

你需要在你的ASP.NET核心項目,使CORS。有這方面的信息:https://docs.microsoft.com/en-us/aspnet/core/security/cors

你需要調用AddCorsConfigureServices

services.AddCors(); 

然後UseCorsConfigure:當你使用端口9000

// Shows UseCors with CorsPolicyBuilder. 
app.UseCors(builder => builder.WithOrigins("http://example.com")); 

,你對不同來源的API ,但與9001,你是在同一個來源,因此CORS將不適用。

OPTIONS請求被稱爲「預檢」。這裏有更多的信息:https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests

+0

這不可能是與CORS問題。我已經允許所有的起源,請參閱我的最新更新 – Reft

+0

您是否還在'IServiceCollection'上調用了'AddCors',並確保'UseCors'在**'UseMvc'之上**? –

+0

也嘗試過與此相同的實現:https://weblog.west-wind.com/posts/2016/Sep/26/ASPNET-Core-and-CORS-Gotchas – Reft