实现自定义身份和在的IPrincipal MVC [英] Implementing a Custom Identity and IPrincipal in MVC
问题描述
我有一个基本的MVC 2测试版的应用程序,我想实现一个自定义的身份和校长课程。
I have a basic MVC 2 beta app where I am trying to implement a custom Identity and Principal classes.
我已经创建了我的班了实现的IIdentity和IPrincipal的接口,实例化他们,然后在Global.asax的Application_AuthenticateRequest分配CustomPrincipal对象我Context.User。
I have created my classes that implement the IIdentity and IPrincipal interfaces, instantiated them and then assigned the CustomPrincipal object to my Context.User in Application_AuthenticateRequest of the Global.asax.
这所有的成功和物体看起来不错。当我开始渲染视图现在页面失败。第一次失败是在下面的行code的默认LogoOnUserControl观点:
This all succeeds and the objects look good. When I begin to render the Views the pages are now failing. The first failure is in the default LogoOnUserControl view on the following line of code:
[ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ]
如果我拉了这一点,它失败,那么在code不同的Html.ActionLink行。
If I pull this out it then fails on a different "Html.ActionLink" line of code.
我收到的错误是:
类型的例外
System.Runtime.Serialization.SerializationException
发生在WebDev.WebHost40.dll但
在用户code没有处理
An exception of type 'System.Runtime.Serialization.SerializationException' occurred in WebDev.WebHost40.dll but was not handled in user code
其他信息:类型不
解决的会员
Model.Entities.UserIdentity,模型,
版本= 1.0.0.0,文化=中立,
公钥=空'。
Additional information: Type is not resolved for member 'Model.Entities.UserIdentity,Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
有没有办法,我需要我的身份来实现,以使用MVC自定义标识一些额外的属性?我试图执行[序列化()]在Identity类,但它似乎并没有产生影响。
Is there some additional properties that I need to implement in my Identity in order to use a custom Identity in MVC? I tried to implement [Serializable()] in the Identity class but it didn't seem to have an impact.
更新:
我试过的实现这个3-4替代方式,但仍失败,出现同样的错误。如果我使用GenericIdentity /的GenericPrincipal类直接它没有错误。
UPDATE: I've tried 3-4 alternate ways of implemented this but still fails with the same error. If I use GenericIdentity/GenericPrincipal classes directly it does not error.
GenericIdentity ident = new GenericIdentity("jzxcvcx");
GenericPrincipal princ = new GenericPrincipal(ident, null);
Context.User = princ;
但是,这让我无处因为我试图用CustomIdentity持有几个属性。如果我实现的IIdentity / IPrincipal的接口或继承GenericIdentity /的GenericPrincipal我CustomIdentity / CustomPrincipal失败上面原来的错误。
But this gets me nowhere since I am trying to use the CustomIdentity to hold a couple of properties. If I implement the IIdentity/IPrincipal interfaces or inherit GenericIdentity/GenericPrincipal for my CustomIdentity/CustomPrincipal it fails with the original error above.
推荐答案
我想通这一个从网上一点点帮助:)的技巧是,你必须实现在你的类ISerializable接口,实现IIdentity的。我希望这有助于挽救别人一些时间:)
I figured this one out with a little help from the web :) The trick is that you have to implement the ISerializable interface in your class that implements IIdentity. I hope this helps save someone else some time :)
类声明:
[Serializable]
public class ForumUserIdentity : IIdentity, ISerializable
的实施ISerializable的:
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (context.State == StreamingContextStates.CrossAppDomain)
{
GenericIdentity gIdent = new GenericIdentity(this.Name, this.AuthenticationType);
info.SetType(gIdent.GetType());
System.Reflection.MemberInfo[] serializableMembers;
object[] serializableValues;
serializableMembers = FormatterServices.GetSerializableMembers(gIdent.GetType());
serializableValues = FormatterServices.GetObjectData(gIdent, serializableMembers);
for (int i = 0; i < serializableMembers.Length; i++)
{
info.AddValue(serializableMembers[i].Name, serializableValues[i]);
}
}
else
{
throw new InvalidOperationException("Serialization not supported");
}
}
#endregion
下面是链接到对更详细的文章功能
这篇关于实现自定义身份和在的IPrincipal MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!