Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

asp.net mvc 2 - Implementing a Custom Identity and IPrincipal in MVC

I have a basic MVC 2 beta app where I am trying to implement a custom Identity and Principal classes.

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.

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") %> ]

If I pull this out it then fails on a different "Html.ActionLink" line of code.

The error I receive is:

An exception of type 'System.Runtime.Serialization.SerializationException' occurred in WebDev.WebHost40.dll but was not handled in user code

Additional information: Type is not resolved for member 'Model.Entities.UserIdentity,Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

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.

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;

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 :)

Class declaration:

[Serializable]
    public class ForumUserIdentity : IIdentity, ISerializable

Implementation for 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

Here is the link to the article that has more detail on the "Feature"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...