Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:36:03+00:00 2026-05-31T21:36:03+00:00

I have a custom principal object that I want to be able to serialize

  • 0

I have a custom principal object that I want to be able to serialize so that I can store it in the Userdata of the FormsAuthentication cookie. I’m trying to use the DataContractJsonSerializer to do this, but when the serialization occurs I just get an empty string (no exceptions).

[DataContract]
public class MyPrincipal : IPrincipal
{
    private readonly MyIdentity _identity;

    public MyPrincipal(MyIdentity identity)
    {
        _identity = identity;
    }

    [DataMember]
    public IIdentity Identity
    {
        get { return _identity; }
        set { }
    }

    public bool IsInRole(string role)
    {
        return _identity.AuthGroups.Contains(role, StringComparer.OrdinalIgnoreCase);
    }

    public bool IsInRole(string[] roles)
    {
        return roles.Any(IsInRole);
    }
}

[DataContract]
public class MyIdentity : IIdentity
{
    private readonly MyCustomData _customData;

    public MyIdentity(MyCustomData customData)
    {
        _customData = customData;
    }

    #region IIdentity properties

    [DataMember]
    public string Name
    {
        get { return Username; }
        set {}
    }

    [DataMember]
    public string AuthenticationType
    {
        get { return "Forms"; }
        set {}
    }

    [DataMember]
    public bool IsAuthenticated
    {
        get { return true; }
        set { }
    }

    #endregion

    #region custom properties

    [DataMember]
    public string FirstName
    {
        get { return _customData.FirstName; } 
        set { }
    }
    [DataMember]
    public string LastName
    {
        get { return _customData.LastName; }
        set { }
    }
    [DataMember]
    public string RedwoodID
    {
        get { return _customData.CedarnetRedwoodID; }
        set { }
    }
    [DataMember]
    public string Username
    {
        get { return _customData.NetworkLogin; }
        set { }
    }
    [DataMember]
    public string CuwasTicket
    {
        get { return _customData.CuwasTicket; }
        set { }
    }
    [DataMember]
    public List<string> AuthGroups
    {
        get { return _customData.GroupMembership; }
        set { }
    }

    #endregion
}

And here’s the code I’m trying to run to seralize it all:

var serializer = new DataContractJsonSerializer(typeof(MyPrincipal), new List<Type> {typeof(MyPrincipal), typeof(MyIdentity)});
var responseStream = new MemoryStream();
serializer.WriteObject(responseStream, user);
string serializedValue = new StreamReader(responseStream).ReadToEnd();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:36:04+00:00Added an answer on May 31, 2026 at 9:36 pm

    You miss one line:

    serializer.WriteObject(responseStream, user);
    responseStream.Position = 0; // This!!
    string serializedValue = new StreamReader(responseStream).ReadToEnd();
    

    Remember serializer writes to the stream and StreamReader starts from the current position which is end of the stream.

    On a separate note, serialising IPrincipal and IIdentity does not look like a good thing to do. Reason being they represent a volatile status which can change at any time (e.g. permission revoked after serialisation).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that implements IAuthorizationPolicy. I set up a custom Principal object
I have a custom security principal object which I set in the global.asax for
I have custom classes that I currently instantiate within App.xaml as resources. I want
I have custom event that has several different subscribers who will all use the
I have custom component that I can place in my layout file (XML) for
I have implemented a custom IPrincipal that I set in protected void Application_PostAuthenticateRequest(Object sender,
I'm trying to have my custom java application use our Active Directory Server for
I have defined a custom IPrincipal and custom IIdentity based on a website that
I have custom coded several enterprise applications for mid to large organizations to use
I have custom gallery. Gallery represents items that are frame layout. There are one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.