目前在抓取域用户以及允许匿名用户访问该页面时遇到问题。正如我在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。