获得域用户同时也允许C#/ ASP.NET中的匿名用户

时间:2016-10-03 18:09:00

标签: c# asp.net authentication authorization identity

目前在抓取域用户以及允许匿名用户访问该页面时遇到问题。正如我在web.config中设置的那样,它使匿名用户登录。我只想允许任何人,但如果它存在则抓住域用户。

的Web.config

<system.web>
    <authentication mode="Windows" />
    <authorization>
        <!--<allow users="*" />-->
        <deny users="?" />
    </authorization>
    <identity impersonate="true" />
</system.web>

上面是我可以用来抓取域用户的唯一设置,但它要求匿名用户仍然登录。

尝试两种方法来抓取域用户:

string CurrentUser = System.Web.HttpContext.Current.User.Identity.Name.ToString();
string CurrentUser2 = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

我是不是错了?

编辑:按照@MethodMan的说法,我正在使用UserPrincipal。

UserPrincipal CurrentUser = UserPrincipal.Current; 
ViewBag.CurrentUser = CurrentUser.DisplayName.ToString();

但是,我在IIS上遇到以下错误,但不是IIS Express:

Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.

不确定这意味着什么,因为我只使用UserPrincipal。

0 个答案:

没有答案