我尝试在我们现有的数据库之上实现 ASP.NET 身份验证和授权。我们有一个网站调用网络服务来获取其数据。要使用网络服务,我需要提供用户名和密码。知道了这一点,我决定实现 IIdentity 和 IPrincipal 来存储加密的密码,并能够在执行 web 服务调用时提供它。将来,我们可能希望更多地使用 asp.net 的内置安全性,因此我实现了成员资格和角色提供程序并覆盖了我所需要的(ValidateUser 和 GetRoles)虽然,在验证用户之后感谢成员资格提供程序实现我仍然将我自己的 CustomIdentity 设置为 Context.User 以便能够在需要时检索其密码。
只要允许用户访问该页面,它就可以完美运行。但是当用户被拒绝时,框架不会抛出 AccessDeniedException,而是在我的 CustomIdentity 上抛出序列化异常。我发现了一个完全相似的行为,并在此链接上描述了更多详细信息,但没有发布任何答案。
我的例外与上面的链接完全相同
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 是否正确?如果没有,如果我使用成员资格和角色提供者,在哪里添加密码或其他用户数据等属性?
最好的祝福,
斯蒂芬·埃布雷希