Home Dashboard Directory Help
Search

Using Custom Identities in ASP.NET fails when using the ASP.NET Developement Server by Paulo Morgado


Status: 

Closed
 as Won't Fix Help for as Won't Fix


27
0
Sign in
to vote
Type: Bug
ID: 274696
Opened: 4/29/2007 12:32:09 PM
Access Restriction: Public
4
Workaround(s)
view
20
User(s) can reproduce this bug

Description

When using custom identities (classes that implement System.Security.Principal.IIdentity) and the ASP.NET Development Server a SerializationException is thrown.
Details
Sign in to post a comment.
Posted by BaggaDonuts on 6/28/2012 at 2:18 PM
Here's how you fix this issue. First, mark your class as Serializable. then implement the ISerializable interface:

/// <summary>
/// Stubs identity for offline authentication
/// </summary>
[Serializable]
public class StubIdentity : IIdentity, ISerializable

...

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");
    }
}
Posted by jvella1975 on 6/27/2012 at 4:29 AM
Come on microsoft .. this bug has existed for five years and through 3 (about to be 4) versions of visual studio ... fix it or explain why you won't/can't.

"Your feedback improving Microsoft products" <-- yeah .. apparently not so much :(
Posted by Reto Ghioldi on 5/21/2012 at 12:58 AM
"We will consider this issue again in a future release."...

Will this issue be fixed in Visual Studio 11? Hope so! :-)
Posted by BLoritsch on 10/5/2010 at 11:29 AM
This problem not only still exists, it also affects IPrincipal classes. When your IPrincipal is implemented using Linq to Entities, none of the provided workarounds can work. I can't change the inheritance hierarchy to include MarshalByRefObject. The IIdentity workaround no longer works because it is an IPrincipal. And I'm using C#, not VB so that workaround does not work.

If this bug cannot be fixed, it would help to understand what is preventing the ASP.NET development server from functioning properly.
Posted by mt1337 on 3/19/2010 at 10:48 AM
Oh I see MS has closed the issue and has no interest in fixing it "for this release" (whatever that means).

How cute.
Posted by mt1337 on 3/19/2010 at 10:47 AM
Is Microsoft going to fix this bug, EVER? It's 2010, and VS 2008 with SP1 has the issue still.
Posted by Travis Illig on 1/29/2010 at 3:38 PM
This happens while using Microsoft.IdentityModel.Claims.ClaimsPrincipal, too. Apparently claims-based auth systems won't be supported on the dev server.
Posted by Microsoft on 6/29/2007 at 3:36 PM
Thank you for your feedback. We are aware of this issue and are not planning to address this in the ASP.NET Development Server for this release. We will consider this issue again in a future release.

The Web Tools Team
Posted by Microsoft on 5/3/2007 at 1:23 PM
Thanks for submitting this issue. We are investigating.

The Web Tools Team
Posted by Microsoft on 4/29/2007 at 11:32 PM
Thanks for your feedback. We have reproduced this bug on Win2003 SP2 and OrcasBeta1VSTS, and we are sending this bug to the appropriate group within the Visual Studio Product Team for triage and resolution. Thank you, Visual Studio Product Team.
Posted by Microsoft on 4/29/2007 at 6:31 PM
Thank you for your feedback. We are currently investigating. If this issue is urgent, please call support directly (see http://support.microsoft.com). Thank you, Visual Studio Product Team.
Posted by Paulo Morgado on 4/29/2007 at 2:13 PM
If anyone wants to try this, he/she can read this blog post (http://msmvps.com/blogs/paulomorgado/archive/2007/04/29/bug-using-custom-identities-in-asp-net-fails-when-using-the-asp-net-developement-server.aspx) and download the sample.
Sign in to post a workaround.
Posted by BaggaDonuts on 6/28/2012 at 2:19 PM
Here's how you fix this issue. First, mark your class as Serializable. then implement the ISerializable interface:

/// <summary>
/// Stubs identity for offline authentication
/// </summary>
[Serializable]
public class StubIdentity : IIdentity, ISerializable

...

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");
    }
}
Posted by simbolo on 3/22/2009 at 6:33 PM
When using a custom IIdentity, the following will produce a SerializationException on the ASP.NET Development Server (VS2008).

Dim UserIP as String = HttpContext.Current.Request.UserHostAddress

I would except the UserIP to become something like "127.0.0.1".

Workaround...
Dim UserIP as String="127.0.0.1"
dim HostName as string= HttpContext.Current.Request.Url.Host.ToString.ToLower
If Not Hostname = "localhost" Then
UserIP = HttpContext.Current.Request.UserHostAddress
End If
Posted by Leandro Díaz Guerra on 12/18/2007 at 9:56 AM
Another possibility, which works great even if it adds a small performance drop, is to make the class heritate from MarshalByRefObject. If you do this, it will work correctly in both Cassini and IIS.
Posted by ps279 on 9/19/2007 at 12:34 AM
Implement ISerializable on your IIdentity class. Implement GetObjectData with code like this:

if (IsSet(context.State, StreamingContextStates.CrossAppDomain))
{
    info.SetType(typeof(GenericIdentity));
    info.AddValue("m_name", name);
    info.AddValue("m_type", AUTHENTICATIONTYPE);
    return;
}

This seems to be good enough to get my app running on the ASP.NET development server. This may have unpredictable effects in other environments.