gpt4 book ai didi

c# - 自定义Identity和GenericPrincipal引起的序列化异常

转载 作者:行者123 更新时间:2023-11-30 17:15:44 26 4
gpt4 key购买 nike

我一直在关注一篇关于如何使用 .net 实现自定义基本身份验证的文章。

这是 article

该代码使用一个接口(interface) - IBasicUser - 派生自 IIdentity。BasicUser 类实现了 IBasicUser。创建 BasicUser 的一个实例,并从中构造一个 GenericPrincipal。

IBasicUser bu = new BasicUser();
context.Context.User = new GenericPrincipal(bu, new string[] { });

当我运行代码时出现异常。

Type 'Smithfamily.Blog.Samples.BasicUser' in assembly 'LM_TESTS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. 

于是我查看了 Reflector 中的 System.Security.Principal.GenericIdentity 类,发现它确实标有 [Serializable. ComVisible(真)]

Documentation关于 IIdentity 没有说明需要序列化的实现。从异常和 Reflector 我假设它确实如此。所以我将 [Serializable] 属性添加到 BasicUser 类。

现在我在运行代码时得到一个新的异常。

[SerializationException: Type is not resolved for member 'Smithfamily.Blog.Samples.BasicUser,LM_TESTS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.]

我对 Serialization 不是很熟悉,也不太明白这是怎么回事,问题是什么。请你能帮我开导一下吗。谢谢。

最佳答案

您是否在 VS Web 开发服务器中托管您的网站?如果是这样,看起来您偶然发现了 MS Connect 中记录的错误...

http://connect.microsoft.com/VisualStudio/feedback/details/274696/using-custom-identities-in-asp-net-fails-when-using-the-asp-net-developement-server

如果我正确阅读这篇文章和其他关于它的帖子,修复方法是对您的 DLL 进行 GAC 处理,以便它可以通过 .NET 探测正确找到并在运行时加载。

关于c# - 自定义Identity和GenericPrincipal引起的序列化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7797973/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com