asp.net - 在没有登录提示的情况下通过 Angular 2 访问 Windows Authenticated Web API

标签 asp.net angular asp.net-web-api windows-authentication

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

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

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

这是一个示例 TS 脚本,它从 Angular 服务向经过 Windows 身份验证的 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);
    }

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

我想在没有这个登录名的情况下访问它们...

最佳答案

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

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

这是做到这一点的方法。

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

Screenshot is here...

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

关于asp.net - 在没有登录提示的情况下通过 Angular 2 访问 Windows Authenticated Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48745373/

相关文章:

javascript - 使 asp.net webforms 母版页上的 js 代码中的路径(目录)与根目录中不同级别的页面一起使用

asp.net - TableSorter 丢失标题文本

jquery - 将多个参数传递给我的 webAPI Controller 的 'POST' ajax 调用> http 404 not found

c# - JQuery Ajax 从客户端上传文件和其他值

javascript - 使用 javascript 更改点击时的图像大小

angular - ionic 2 : How to make a sliding menu from the bottom

angular - 将 observable 运行到 zone.js 中

javascript - 绘图工具 - Angular2

c# - 如何让 NUnit 测试项目使用自定义异常过滤器?

asp.net - web api TaskScheduler.UnobservedTaskException