BUG: Using Custom Identities in ASP.NET fails when using the ASP.NET Developement Server

This bug has been around for a while and affects web site development when using custom identities (classes that implement System.Security.Principal.IIdentity) and the ASP.NET Development Server.

To reproduce this bug, you have to implement the IIdentity interface, something like this:

[Serializable]

public class MyIdentity : System.Security.Principal.IIdentity

{

    private string name;

    private string authenticationType;

    private bool isAuthenticated;

 

    public MyIdentity(string name, string authenticationType, bool isAuthenticated)

    {

        this.name = name;

        this.authenticationType = authenticationType;

        this.isAuthenticated = isAuthenticated;

    }

 

    #region IIdentity Members

 

    public string AuthenticationType

    {

        get { return this.authenticationType; }

    }

 

    public bool IsAuthenticated

    {

        get { return this.isAuthenticated; }

    }

 

    public string Name

    {

        get { return this.name; }

    }

 

    #endregion

}

And use it somewhere like this:

        protected void Application_PostAuthenticateRequest(object sender, EventArgs e)

        {

            Thread.CurrentPrincipal = HttpContext.Current.User =

                new GenericPrincipal(new MyIdentity(HttpContext.Current.User.Identity.Name, “test”, true), new string[0]);

        }

It doesn’t happen all the time but it happens. Eventually, you’ll get this in your event log:

Event Type:    Warning

Event Source:    ASP.NET 2.0.50727.0

Event Category:    Web Event

Event ID:    1309

Date:        29-04-2007

Time:        18:34:54

User:        N/A

Computer:    ORCASBETA1VSTS

 

Description:

   Event code: 3005

 

Event message: An unhandled exception has occurred.

   Event time: 29-04-2007 18:34:54

   Event time (UTC): 30-04-2007 1:34:54

   Event ID: 8afb62b7b7604da087509636bb48fbad

   Event sequence: 16

   Event occurrence: 1

   Event detail code: 0

 

Application information:

   Application domain: 53ad158c-10-128223704725800576

   Trust level: Full

   pplication Virtual Path: /WebSite3

   Application Path: C:\Documents and Settings\Administrator\My Documents\Visual Studio Codename Orcas\WebSites\WebSite3\

   Machine name: ORCASBETA1VSTS

 

 

Process information:

   Process ID: 3880

   Process name: WebDev.WebServer.EXE

   Account name: ORCASBETA1VSTS\Administrator

 

Exception information:

   Excpetion type: SerializationException

   Exception message: Type is not resolved for member ‘MyIdentity,App_Code.4broaqvc, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’.

 

 

Request information:

   Request URL: http://localhost:1137/WebSite3/default.aspx

   Request path: /WebSite3/default.aspx

   User host address: 127.0.0.1

   User: user

   Is authenticated: True

   Authentication Type: test

   Thread account name: ORCASBETA1VSTS\Administrator

 

Thread information:

   Thread ID: 4

   Thread account name: ORCASBETA1VSTS\Administrator

   Is impersonating: False

 

Stack trace:

   at Microsoft.VisualStudio.WebHost.Connection.ReadRequestBytes(Int32 maxBytes)

   at Microsoft.VisualStudio.WebHost.Request.ReadEntityBody(Byte[] buffer, Int32 size)

   at System.Web.HttpRequest.GetEntireRawContent()

   at System.Web.HttpRequest.FillInFormCollection()

   at System.Web.HttpRequest.get_Form()

   at System.Web.HttpRequest.get_HasForm()

   at System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull)

   at System.Web.UI.Page.DeterminePostBackMode()

   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

I’ve opened a bug on this. If you are having the same problem, vote on it.

With the bug, I’ve submitted this sample.

2 thoughts on “BUG: Using Custom Identities in ASP.NET fails when using the ASP.NET Developement Server”

  1. If I correctly understand your problem appear to be a serialization problem with your MyIdentity class

    I have a kind of problem with my web site. Really difficult to resolve for me

    You may find a better explaination there

    http://www.lhotka.net/weblog/CommentView,guid,cfcaf6c4-63cf-4cf1-8361-ed3db07496a4.aspx

    I code that serialization implementation to my equivalent MyIdentity class and it resolve my problem

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
    // http://www.lhotka.net/weblog/CommentView,guid,cfcaf6c4-63cf-4cf1-8361-ed3db07496a4.aspx
    if (context.State == StreamingContextStates.CrossAppDomain)
    {
    GenericIdentity generic = new GenericIdentity(this.Name, this.AuthenticationType);

    info.SetType(generic.GetType());

    MemberInfo[] serializableMembers = FormatterServices.GetSerializableMembers(generic.GetType());

    Object[] serializableValues = FormatterServices.GetObjectData(generic, serializableMembers);

    for (int i = 0; serializableMembers.Length > i; i++)
    {
    info.AddValue(serializableMembers[i].Name, serializableValues[i]);
    }
    }
    else
    {
    throw new ApplicationException(“Serialization not supported”) ;
    }
    }

    I hope it’ll help you like me !

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>