C# IIS错误“;无法强制转换类型为';System.DirectoryServices.AccountManagement.GroupPrincipal'&引用;

C# IIS错误“;无法强制转换类型为';System.DirectoryServices.AccountManagement.GroupPrincipal'&引用;,c#,asp.net,visual-studio-2010,C#,Asp.net,Visual Studio 2010,示例代码: staffName.Text = UserPrincipal.Current.Name; Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'. [InvalidCastException: Unable to cast o

示例代码:

staffName.Text = UserPrincipal.Current.Name;
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
您好,我从vs2010将项目发布到IIS时出现此错误。有人能帮我吗,即使我尝试删除代码和引用,我仍然会遇到这个错误(这很奇怪)

错误:

staffName.Text = UserPrincipal.Current.Name;
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
堆栈跟踪:

staffName.Text = UserPrincipal.Current.Name;
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

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

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

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

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

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

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

很好!它设法跑过,但什么也不显示。(如“”)必须通过身份验证方法设置当前用户。如果你是匿名登录,我相信它将是空白的。我得到了上面的空白。很好!它设法跑过,但什么也不显示。(如“”)必须通过身份验证方法设置当前用户。如果你是匿名登录,我相信它将是空白的。我得到了上面的空白。