c#.net Web Api Windows和匿名身份验证获取当前用户 [英] c# .net Web Api Windows and Anonymous Authentication get current user

查看:836
本文介绍了c#.net Web Api Windows和匿名身份验证获取当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取本地用户,但我没有回报.

I'm trying to get the local user, but I have no return.

  • WebApi
  • IIS(10)Windows 10

启用了我的IIS:

  • Windows
  • 基本
  • 匿名

Visual Studio 2017 C#.net 4.6.1

Visual Studio 2017 C# .net 4.6.1

我已经尝试标记Web配置:

I already tried to tag the web config:

  <authorization>
  <allow users="?" />
</authorization>
AND
<identity impersonate="false" />
<authentication mode="Windows" />

代码WebApi

 [HttpGet]
    [Authorize]//I already tried [AllowAnonymous]
    public User Get()
    {
        System.Net.NetworkCredential SystemCredential = (System.Net.NetworkCredential)ADUtil.SystemCredential();
        string login = HttpContextUtil.Request.LogonUserIdentity.Name;
error here, login return 'NT AUTHORITY\IUSR'.
}

推荐答案

此处所述,如果要允许用户要使用Windows身份验证来请求匿名和其他,最好的方法是为每个AuthenticationScheme呈现两个不同的子路径,例如为匿名请求(如/anonymous/{controller}/{action})定义一条路由,为Windows身份验证请求/windows/{controller}/{action}定义一个路由.因此,您不必双重实现控制器,但缺点是必须为同一模块运行两个连接.

As mentioned here if you want to allow users to request anonymous and others with windows authentication, your best way is to present two different sub-paths for each AuthenticationScheme for example define a route for anonymous request like /anonymous/{controller}/{action} and one for windows authentication requests /windows/{controller}/{action}. Therefor you don't have to double implement the controller, but the drawback is that you have to run two connections for the same module.

这篇关于c#.net Web Api Windows和匿名身份验证获取当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆