2017-08-15 54 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

相关问题