C# 无法将“System.Security.Principal.WindowsIdentity”类型的对象强制转换为自定义类
在VS2013中使用WebForms在C for ASP.net 4中开发时,我实现了一个自定义的安全对象,该对象已在我所有的应用程序中使用。在从VS2010升级到VS2013的过程中,我遇到了这个让我非常头疼的错误 无法强制转换类型为的对象 要键入的“System.Security.Principal.WindowsIdentity” 'CustomSecurity.BusinessObject.EmployeeIdentity' 代码位置如下所示:C# 无法将“System.Security.Principal.WindowsIdentity”类型的对象强制转换为自定义类,c#,asp.net,visual-studio-2010,visual-studio,C#,Asp.net,Visual Studio 2010,Visual Studio,在VS2013中使用WebForms在C for ASP.net 4中开发时,我实现了一个自定义的安全对象,该对象已在我所有的应用程序中使用。在从VS2010升级到VS2013的过程中,我遇到了这个让我非常头疼的错误 无法强制转换类型为的对象 要键入的“System.Security.Principal.WindowsIdentity” 'CustomSecurity.BusinessObject.EmployeeIdentity' 代码位置如下所示: public partial class
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//This is where it fails
EmployeeIdentity employeeIdentity = (EmployeeIdentity)HttpContext.Current.User.Identity;
}
}
这行代码在VS2010中起作用,但在VS2013中不起作用。代码可以编译,但在运行时出现此错误
到目前为止我已经尝试过的事情:
将CustomSecurity.BusinessObject.EmployeeIdentity转换为.net 4程序集
将代码更改为使用Page。用户和我仍然会收到相同的错误
阅读StackOverflow中的其他帖子,没有找到答案。。。。
我的问题:
还有什么其他建议可以解决我的问题?因此,将代码从VS2010转换为VS2013后,我需要的更改是将以下内容添加到Web.config:
<system.webServer>
<modules>
<!-- Add a Custom Authentication module THIS NEEDS TO BE ADDED FOR IIS8 + .NET 4 -->
<add name="DHAAuthenticationModule" type=" DHA.CustomSecurity.BusinessObject.DHAAuthenticationModule, DHA.CustomSecurity.BusinessObject"/>
</modules>
</system.webServer>
我假设的原因是Web服务器也需要理解对象定义。。。。
这修复了我遇到的错误。我想这是非常具体的
对此有何想法?在设置当前用户标识的位置抛出一个断点,然后查看该断点是否不再触发。另外,请仔细检查您的IIS/IISExpress安全性,以确保您仍然使用正确的匿名/Windows/Forms/Basic身份验证值进行设置。能否显示您试图获取/存储当前用户的代码。。?是否使用主上下文?请显示代码和发生错误的地方,应考虑使用“类型”代替type@JoeEnosWindows身份验证已启用,匿名已禁用。这是我在VS2010中的其他应用程序中使用的设置。适用的代码将是将当前主体设置为EmployeeIdentity实例的代码。它可能位于全局或自定义模块中。