簡體   English   中英

Windows 身份驗證和 Angular 7 應用程序

[英]Windows Authentication and Angular 7 application

我開發了內網應用

后端:ASP.NET WEB API-2(所有控制器都具有授權屬性),前端:Angular 7(在生產構建后我將生成的腳本移動到我的后端項目):

....
  <app-root> 
      <div id="preloader"></div>
  </app-root>


  <script type="text/javascript" src="~/Scripts/SPA/runtime.26209474bfa8dc87a77c.js"></script>
  <script type="text/javascript" src="~/Scripts/SPA/es2015-polyfills.bda95d5896422d031328.js" nomodule></script>
  <script type="text/javascript" src="~/Scripts/SPA/polyfills.8bbb231b43165d65d357.js"></script>
  <script type="text/javascript" src="~/Scripts/SPA/main.122a2bd84f391ab8df1d.js"></script>
</body>

問題是在部署到服務器后提示輸入我的用戶名/密碼。如果用戶輸入憑據,它工作正常,但我希望應用程序自動獲取登錄的用戶。

這是我的 web.config

<authentication mode="Windows" />
   <authorization>
     <deny users="?" />
   </authorization>

這是我的角度攔截器

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class CredentialsInterceptor implements HttpInterceptor {
    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        request = request.clone({
            withCredentials: true
        });

        return next.handle(request);
    }
}

在 Visual Studio 2019 項目設置中

匿名身份驗證:已啟用

Windows 身份驗證:已啟用

托管管道模式:集成

全球.asax

protected void Application_BeginRequest(Object sender, EventArgs e)
       {
           //Preflight request comes with HttpMethod OPTIONS
           //The following line solves the error message
           //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:4202");
           HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true");
           if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
           {
               HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
               HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
               HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Pragma, Cache-Control, Authorization");
               HttpContext.Current.Response.End();
           }
       }

您還需要在跨域請求中允許SupportsCredentials

服務器端(Web API):

[EnableCors]屬性上將SupportsCredentials屬性設置為true

[EnableCors(origins: "http://exampleclient.com", headers: "*", 
methods: "*", SupportsCredentials = true)]

當您輸入憑據時一切正常,這一事實意味着這是客戶端問題,而不是服務器端問題。 瀏覽器不會自動發送您的憑據。

僅當站點位於 Internet 選項中的受信任站點列表中時,Chrome 和 IE 才會自動發送當前登錄用戶的憑據。

  1. 打開開始菜單。
  2. 輸入“Internet 選項”並單擊它。
  3. 單擊“安全”選項卡。
  4. 單擊“受信任的站點”圖標。
  5. 單擊“站點”按鈕。
  6. 將您網站的域添加到列表中。 您可以使用通配符。

這也可以通過組策略設置,因此可以將設置推送到組織中的每台計算機。 請參閱此處的答案。

Firefox 使用自己的設置: network.negotiate-auth.delegation-uris 我相信這也可以通過組策略設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM