在 Web api 中正确实施 "windows" 身份验证?
proper implementation of "windows" authentication in web api?
我创建了一个 Web Api 2 应用程序,它只能在公司网络上使用。我已经在 Web API 中阅读了有关 Windows 身份验证的信息,所以这似乎是可能的。但是我需要为此找出正确的实现方式。我在我的 Web.config 中包含了以下 xml:
<system.web>
<authentication mode="Windows" />
</system.web>
我似乎记得老式网络表单应用程序中的某种事件挂钩。类似于 BeginRequest() 之类的东西,可以在呈现页面之前进行安全检查。我在我的一个控制器方法中包含了以下代码行作为第一行,但返回值似乎只是一个空的 object,没有任何有意义的信息:
var identity = HttpContext.Current.User.Identity as WindowsIdentity;
Web API 2 是否支持 Windows 身份验证?我错过了一步吗?如果我提交来自 Postman 的一般测试请求,Windows 身份验证是否应该起作用?我也试过这段代码,但得到了类似的空 object:
var x = RequestContext.Principal;
我依稀记得像 "Enable Integrated Security." 这样的 IIS 设置,您能指定确切的设置吗?如果我是 运行 IIS Express 上的应用程序,我能否完成此操作?
更新
我按照以下答案之一中提到的 IIS Express 的步骤进行操作,但我在原始 post 中提供的代码示例仍然没有获得填充用户 object。我还更新了 applicationhost.config 文件以关闭匿名身份验证:
<anonymousAuthentication enabled="false" userName="" />
进行更新后,我通过 Postman 重新提交了我的测试请求,但出现以下错误:
<h3>HTTP Error 401.2 - Unauthorized</h3>
<h4>You are not authorized to view this page due to invalid authentication headers.</h4>
</div>
<div class="content-container">
<fieldset>
<h4>Most likely causes:</h4>
<ul>
<li>No authentication protocol (including anonymous) is selected in IIS.</li>
<li>Only integrated authentication is enabled, and a client browser was used that does not support integrated authentication.</li>
<li>Integrated authentication is enabled and the request was sent through a proxy that changed the authentication headers before they reach the Web server.</li>
<li>The Web server is not configured for anonymous access and a required authorization header was not received.</li>
<li>The "configuration/system.webServer/authorization" configuration section may be explicitly denying the user access.</li>
</ul>
</fieldset>
</div>
<div class="content-container">
<fieldset>
<h4>Things you can try:</h4>
<ul>
<li>Verify the authentication setting for the resource and then try requesting the resource using that authentication method.</li>
<li>Verify that the client browser supports Integrated authentication.</li>
<li>Verify that the request is not going through a proxy when Integrated authentication is used.</li>
<li>Verify that the user is not explicitly denied access in the "configuration/system.webServer/authorization" configuration section.</li>
<li>Check the failed request tracing logs for additional information about this error. For more information, click
<a href="http://go.microsoft.com/fwlink/?LinkID=66439">here</a>.
</li>
</ul>
</fieldset>
</div>
我是否需要使用某种类型的特殊 header 配置我的 Postman 请求才能使其正常工作?
如果您正在使用 IIS Express,您需要更新 applicationhost.config
文件。
这是 IIS 配置工具的文件版本,您可以在其中配置 Web 服务器本身。您可以在以下目录中找到此文件:
%userprofile%\documents\iisexpress\config\applicationhost.config
或
%userprofile%\my documents\iisexpress\config\applicationhost.config
找到后更新为:
<windowsAuthentication enabled="true">
<providers>
<add value="Negotiate" />
<add value="NTLM" />
</providers>
</windowsAuthentication>
对于 IIS:
- Select 你的申请
- 双击 - 'Authentication'
- 启用Windows身份验证
- 重新启动 IIS 服务器
查看更多内容
Windows 使用本地域用户且用于 Intranet 站点的身份验证。
示例:
我实现了一个 TestAuthentication
method/action 具有固定的路由路径。对于演示,我还没有包括 Authorize 属性。该代码检查 ApiController
的 User
属性。这包含与 Thread.CurrentPrincipal
或 HttpContext.Current.User
相同的数据。确保禁用 IIS 中的匿名身份验证,否则 Identity.Name
将为空。
public class WinAuthController : ApiController
{
[HttpGet]
[Route("api/testauthentication")]
public IHttpActionResult TestAutentication()
{
Debug.Write("AuthenticationType:" + User.Identity.AuthenticationType);
Debug.Write("IsAuthenticated:" + User.Identity.IsAuthenticated);
Debug.Write("Name:" + User.Identity.Name);
if (User.Identity.IsAuthenticated)
{
return Ok("Authenticated: " + User.Identity.Name);
}
else
{
return BadRequest("Not authenticated");
}
}
}
在 Web.config 文件中:
<system.web>
<authentication mode="Windows" />
</system.web>
在 IE 中,您可以使用“工具”>“Internet 选项”>“高级”检查设置,并查找设置“启用 Windows 集成身份验证”。当您转到“安全”选项卡,然后转到“内部网”和“自定义级别”时,您会在底部找到一个设置,用于指定 IE 是自动登录还是提示输入用户名和密码。
请访问下面 link,它有正确的 WEP API Windows 身份验证步骤:
以下是在 Web api 中为本地和服务器 (IIS) 配置 windows 身份验证的步骤。
1) 本地:
a) 要在 windows 身份验证模式下创建 Web api 项目,请按照以下步骤操作:
在选择 ASP.Net Web 应用程序、select Web API 模板 之后右侧单击 Change Authentication 按钮和 select Windows Authentication.
b) 对于现有的网络 api 项目,只需在 applicationhost.config
文件中添加以下行。
<location path="YourProjectName">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="false" />
<windowsAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
</location>
2) 对于服务器 (IIS)
在 IIS 中托管应用程序后,要进行 运行 windows 身份验证,只需在 system.web
节点内的 web.config
文件中添加以下行:
<authentication mode="Windows" />
<authorization>
<allow verbs="OPTIONS" users="?" />
<deny users="?" />
</authorization>
在这两种情况下,只需在您的代码中使用以下行即可 windows 身份验证正常工作:
if(User.Identity.IsAuthenticated)
{
//do work
}
除了前面的回答,我们还需要在跨域请求中传递凭证。
服务器端(Web API):
在 [EnableCors]
属性上将 SupportsCredentials 属性 设置为 true
:
[EnableCors(origins: "http://exampleclient.com", headers: "*",
methods: "*", SupportsCredentials = true)]
客户端 (UI):
将XMLHttpRequest.withCredentials设置为true
。
jQuery:
$.ajax({
type: 'get',
url: 'http://www.example.com/api/auth',
xhrFields: {
withCredentials: true
}
Angular:
this.http.get('http://www.example.com/api/auth', { withCredentials: true }).subscribe((resp: any) => {
console.log(resp)
}
XMLHttpRequest:
var xhr = new XMLHttpRequest();
xhr.open('get', 'http://www.example.com/api/auth');
xhr.withCredentials = true;
我创建了一个 Web Api 2 应用程序,它只能在公司网络上使用。我已经在 Web API 中阅读了有关 Windows 身份验证的信息,所以这似乎是可能的。但是我需要为此找出正确的实现方式。我在我的 Web.config 中包含了以下 xml:
<system.web>
<authentication mode="Windows" />
</system.web>
我似乎记得老式网络表单应用程序中的某种事件挂钩。类似于 BeginRequest() 之类的东西,可以在呈现页面之前进行安全检查。我在我的一个控制器方法中包含了以下代码行作为第一行,但返回值似乎只是一个空的 object,没有任何有意义的信息:
var identity = HttpContext.Current.User.Identity as WindowsIdentity;
Web API 2 是否支持 Windows 身份验证?我错过了一步吗?如果我提交来自 Postman 的一般测试请求,Windows 身份验证是否应该起作用?我也试过这段代码,但得到了类似的空 object:
var x = RequestContext.Principal;
我依稀记得像 "Enable Integrated Security." 这样的 IIS 设置,您能指定确切的设置吗?如果我是 运行 IIS Express 上的应用程序,我能否完成此操作?
更新
我按照以下答案之一中提到的 IIS Express 的步骤进行操作,但我在原始 post 中提供的代码示例仍然没有获得填充用户 object。我还更新了 applicationhost.config 文件以关闭匿名身份验证:
<anonymousAuthentication enabled="false" userName="" />
进行更新后,我通过 Postman 重新提交了我的测试请求,但出现以下错误:
<h3>HTTP Error 401.2 - Unauthorized</h3>
<h4>You are not authorized to view this page due to invalid authentication headers.</h4>
</div>
<div class="content-container">
<fieldset>
<h4>Most likely causes:</h4>
<ul>
<li>No authentication protocol (including anonymous) is selected in IIS.</li>
<li>Only integrated authentication is enabled, and a client browser was used that does not support integrated authentication.</li>
<li>Integrated authentication is enabled and the request was sent through a proxy that changed the authentication headers before they reach the Web server.</li>
<li>The Web server is not configured for anonymous access and a required authorization header was not received.</li>
<li>The "configuration/system.webServer/authorization" configuration section may be explicitly denying the user access.</li>
</ul>
</fieldset>
</div>
<div class="content-container">
<fieldset>
<h4>Things you can try:</h4>
<ul>
<li>Verify the authentication setting for the resource and then try requesting the resource using that authentication method.</li>
<li>Verify that the client browser supports Integrated authentication.</li>
<li>Verify that the request is not going through a proxy when Integrated authentication is used.</li>
<li>Verify that the user is not explicitly denied access in the "configuration/system.webServer/authorization" configuration section.</li>
<li>Check the failed request tracing logs for additional information about this error. For more information, click
<a href="http://go.microsoft.com/fwlink/?LinkID=66439">here</a>.
</li>
</ul>
</fieldset>
</div>
我是否需要使用某种类型的特殊 header 配置我的 Postman 请求才能使其正常工作?
如果您正在使用 IIS Express,您需要更新 applicationhost.config
文件。
这是 IIS 配置工具的文件版本,您可以在其中配置 Web 服务器本身。您可以在以下目录中找到此文件:
%userprofile%\documents\iisexpress\config\applicationhost.config
或
%userprofile%\my documents\iisexpress\config\applicationhost.config
找到后更新为:
<windowsAuthentication enabled="true">
<providers>
<add value="Negotiate" />
<add value="NTLM" />
</providers>
</windowsAuthentication>
对于 IIS:
- Select 你的申请
- 双击 - 'Authentication'
- 启用Windows身份验证
- 重新启动 IIS 服务器
查看更多内容
Windows 使用本地域用户且用于 Intranet 站点的身份验证。
示例:
我实现了一个 TestAuthentication
method/action 具有固定的路由路径。对于演示,我还没有包括 Authorize 属性。该代码检查 ApiController
的 User
属性。这包含与 Thread.CurrentPrincipal
或 HttpContext.Current.User
相同的数据。确保禁用 IIS 中的匿名身份验证,否则 Identity.Name
将为空。
public class WinAuthController : ApiController
{
[HttpGet]
[Route("api/testauthentication")]
public IHttpActionResult TestAutentication()
{
Debug.Write("AuthenticationType:" + User.Identity.AuthenticationType);
Debug.Write("IsAuthenticated:" + User.Identity.IsAuthenticated);
Debug.Write("Name:" + User.Identity.Name);
if (User.Identity.IsAuthenticated)
{
return Ok("Authenticated: " + User.Identity.Name);
}
else
{
return BadRequest("Not authenticated");
}
}
}
在 Web.config 文件中:
<system.web>
<authentication mode="Windows" />
</system.web>
在 IE 中,您可以使用“工具”>“Internet 选项”>“高级”检查设置,并查找设置“启用 Windows 集成身份验证”。当您转到“安全”选项卡,然后转到“内部网”和“自定义级别”时,您会在底部找到一个设置,用于指定 IE 是自动登录还是提示输入用户名和密码。
请访问下面 link,它有正确的 WEP API Windows 身份验证步骤:
以下是在 Web api 中为本地和服务器 (IIS) 配置 windows 身份验证的步骤。
1) 本地:
a) 要在 windows 身份验证模式下创建 Web api 项目,请按照以下步骤操作:
在选择 ASP.Net Web 应用程序、select Web API 模板 之后右侧单击 Change Authentication 按钮和 select Windows Authentication.
b) 对于现有的网络 api 项目,只需在 applicationhost.config
文件中添加以下行。
<location path="YourProjectName">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="false" />
<windowsAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
</location>
2) 对于服务器 (IIS)
在 IIS 中托管应用程序后,要进行 运行 windows 身份验证,只需在 system.web
节点内的 web.config
文件中添加以下行:
<authentication mode="Windows" />
<authorization>
<allow verbs="OPTIONS" users="?" />
<deny users="?" />
</authorization>
在这两种情况下,只需在您的代码中使用以下行即可 windows 身份验证正常工作:
if(User.Identity.IsAuthenticated)
{
//do work
}
除了前面的回答,我们还需要在跨域请求中传递凭证。
服务器端(Web API):
在 [EnableCors]
属性上将 SupportsCredentials 属性 设置为 true
:
[EnableCors(origins: "http://exampleclient.com", headers: "*",
methods: "*", SupportsCredentials = true)]
客户端 (UI):
将XMLHttpRequest.withCredentials设置为true
。
jQuery:
$.ajax({
type: 'get',
url: 'http://www.example.com/api/auth',
xhrFields: {
withCredentials: true
}
Angular:
this.http.get('http://www.example.com/api/auth', { withCredentials: true }).subscribe((resp: any) => {
console.log(resp)
}
XMLHttpRequest:
var xhr = new XMLHttpRequest();
xhr.open('get', 'http://www.example.com/api/auth');
xhr.withCredentials = true;