通过Angular 2访问Windows Authenticated Web API,无需登录提示

Dha*_*aya 6 asp.net windows-authentication asp.net-web-api angular

我已经在Angular2中开发了前端应用程序,在ASP.net Web API中开发了后端.我使用Windows身份验证已启用,因为我想检测请求用户.这两个应用程序都托管在IIS服务器(Windows Server 2012)中.当我加载角度应用程序时,它加载登录提示,并在给出正确的用户凭据时数据加载正确发生 但我想知道一种在没有登录提示的情况下加载它们的方法,自动进行身份验证.

这是我在Web API中检测请求用户的方式.

string user = HttpContext.Current.Request.ServerVariables["LOGON_USER"]; //Get the current user...
userID = user.Split('\\')[1]; 
Run Code Online (Sandbox Code Playgroud)

这是一个示例TS脚本从Angular Services向Windows Authenticated Web API发送请求.

getPersonalInfo(): Observable<IPersonalInfo> {
        return this._http.get(localStorage.getItem('WebApiURL') +"api/PersonalInfo/" , { withCredentials: true })
            .map((response: Response) => <IPersonalInfo>response.json())
            .catch(this.handleError);
    }
Run Code Online (Sandbox Code Playgroud)

重新启动浏览器时,此登录每次都会询问.

在此输入图像描述

我想通过此登录访问它们...

Dha*_*aya 1

实际上,出现此登录提示是因为两种技术(ASP.net 和 Angular 2)之间通过传递 Windows Windows 凭据进行通信。所以调用windows认证的API时需要进行握手认证。

我很幸运通过浏览器更改隐私设置找到了解决方案。所以我能够在没有登录提示的情况下调用 API。

这是执行此操作的方法。

设置 -> 打开代理设置 -> 隐私 -> 站点

截图在这里...

在这里,您可以添加已托管后端 API 的域,如示例所示。之后,它将作为受信任域,而且不会出现任何登录提示。