IIS错误”;无法强制转换类型为';的对象;System.DirectoryServices.AccountMan

本文关键字:对象 System AccountMan DirectoryServices 转换 错误 IIS 类型 | 更新日期: 2023-09-27 18:27:46

示例代码:

staffName.Text = UserPrincipal.Current.Name;

嗨,当我从vs2010将我的项目发布到IIS时,我遇到了这个错误。有人能帮我吗,即使我试图删除代码和引用,我仍然会收到这个错误(这很奇怪)。

错误:

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

堆栈跟踪:

[InvalidCastException: Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.]
   System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue) +57
   System.DirectoryServices.AccountManagement.UserPrincipal.get_Current() +218
   Appraisal.Staffmain.Page_Load(Object sender, EventArgs e) +121
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

IIS错误”;无法强制转换类型为';的对象;System.DirectoryServices.AccountMan

我会将其切换为使用HttpContext.Current.User.Identity.Name属性。

staffName.Text = HttpContext.Current.User.Identity.Name;

这将为您提供web应用程序的当前用户。

相关文章: