在ASP.NET中拒绝用户时, 'CustomIdentity'上的SerializationException

标签 serialization asp.net-membership iprincipal iidentity access-denied

我尝试在现有数据库之上实现ASP.NET身份验证和授权。
我们有一个网站调用网络服务来获取其数据。要使用Web服务,我需要提供用户名和密码。
知道这一点后,我决定实现IIdentity和IPrincipal来存储加密的密码,并能够在执行Web服务调用时提供该密码。
将来,我们可能希望使用asp.net的更多内置安全性,因此我实现了成员资格和角色提供程序,并覆盖了我所需要的内容(ValidateUser和GetRoles)
但是,在通过身份验证提供程序实现验证用户之后,我仍将自己的CustomIdentity设置为Context.User,以便能够在需要时检索其密码。

只要允许用户访问页面,它就可以正常工作。但是当用户被拒绝时,框架不会在我的CustomIdentity上引发序列化异常,而不是引发AccessDeniedException。
我发现了perfectly similar behaviour with more details described on this link,但是没有答案。

我的异常(exception)与上面的链接完全相同
Type is not resolved for member'CW.CustomAuthentication.CWIdentity,CW.CustomAuthentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.Serialization.SerializationException: Type is not resolved for member 'CW.CustomAuthentication.CWIdentity,CW.CustomAuthentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [SerializationException: Type is not resolved for member 'CW.CustomAuthentication.CWIdentity,CW.CustomAuthentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.] Microsoft.VisualStudio.WebHost.Connection.get_RemoteIP() +0 Microsoft.VisualStudio.WebHost.Request.GetRemoteAddress() +65 System.Web.HttpRequest.get_UserHostAddress() +18 System.Web.HttpRequest.get_IsLocal() +13 System.Web.Configuration.CustomErrorsSection.CustomErrorsEnabled(HttpRequest request) +86 System.Web.HttpContext.get_IsCustomErrorEnabled() +42 System.Web.Configuration.UrlAuthFailedErrorFormatter.GetErrorText(HttpContext context) +16 System.Web.Security.UrlAuthorizationModule.WriteErrorMessage(HttpContext context) +29 System.Web.Security.UrlAuthorizationModule.OnEnter(Object source, EventArgs eventArgs) +8777783 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
同时使用成员身份和自定义IIdentity和IPrincipal是否正确?
如果没有,如果使用成员资格和角色提供程序,应在哪里添加密码或其他用户数据之类的属性?

此致,

斯蒂芬·埃布雷希(Stephane Erbrech)

最佳答案

经过更多测试后,根据我发布的链接所说,似乎只有在从Visual Studio以 Debug模式运行时,才会发生此错误。如果我将项目设置为在IIS中运行,则该错误消失并且安全性实现按预期工作。

---那是不是在Visual Studio中实现的轻量级Web服务器中的错误?---

编辑 :
您可以进入Web项目的属性,转到“Web”选项卡,然后选中“使用本地IIS服务器”。但是,这将要求您以管理员身份运行Visual Studio并在计算机上安装IIS,以便VS在加载项目时可以在本地IIS服务器中创建虚拟目录。

关于在ASP.NET中拒绝用户时, 'CustomIdentity'上的SerializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1277371/

相关文章:

c# - 为 asp.net mvc 网站缓存自定义 IPrincipal 的最佳方法是什么?

java - 在序列化代理模式中如何调用代理类的 readResolve() 方法?

java - 使用序列化将对象传递给另一个 JVM - 相同的 Java 版本和 jars(都运行我们的应用程序)

java - 如何从自定义 Gson JsonSerializer 调用另一个序列化程序?

c# - requiresQuestionAndAnswer 在 web.config 中设置为 false 但 RequiresQuestionAndAnswer 在运行时为 true

c# - 使用自定义成员资格和角色提供者在 MVC 中实现 IPrincipal 和 IIdentity

java - 如何删除 Jackson 自定义序列化程序生成的空值?

c# - 在 3 层设置中在哪里可以找到自定义成员资格、角色、配置文件提供程序?

c# - ASP.NET 成员资格 HashAlgorithmType 默认为 HMACSHA256,那么密码散列是键控的吗?

authentication - WebApi ActionFilterAttribute、HttpActionContext 访问用户名(IPrincipal)