3

I have been following an article on how to implement a custom Basic authentication with .net.

This is the article

The code uses an interface - IBasicUser - that derives from IIdentity. The class BasicUser implements IBasicUser. An instance of BasicUser is created and from that a GenericPrincipal is constructed.

IBasicUser bu = new BasicUser();
context.Context.User = new GenericPrincipal(bu, new string[] { });

When I run the code I get an exception.

Type 'Smithfamily.Blog.Samples.BasicUser' in assembly 'LM_TESTS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. 

So I had a look at the System.Security.Principal.GenericIdentity class in Reflector and found that it is indeed marked with the attributes [Serializable. ComVisible(true)]

The Documentation on IIdentity doesn't say anything about implementations needing to be serializable. From the exception and Reflector I assume however that it does. So I add the [Serializable] attribute to the BasicUser class.

Now I get a new exception when I run the code.

[SerializationException: Type is not resolved for member 'Smithfamily.Blog.Samples.BasicUser,LM_TESTS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.]

I am not very familiar with Serialization and don't really understand what is going on or what the problem is. Please could you help enlighten me. Thanks.

Duncan Gravill
  • 4,552
  • 7
  • 34
  • 51
  • 1
    Here's a way you can fix it: http://stackoverflow.com/questions/1884030/implementing-a-custom-identity-and-iprincipal-in-mvc – Donuts Jun 28 '12 at 21:07

3 Answers3

1

Change:

public class CustomIdentity : IIdentity

For:

public class CustomIdentity : MarshalByRefObject, IIdentity

Not use that:

[Serializable]
public class CustomIdentity : IIdentity
1

Are you hosting your web site in the VS Web Development Server? If so, looks like you've stumbled onto a bug logged in MS Connect...

http://connect.microsoft.com/VisualStudio/feedback/details/274696/using-custom-identities-in-asp-net-fails-when-using-the-asp-net-developement-server

If I read this and the other posts about it correctly, the fix is GACing your DLL so that it can be found by .NET probing correctly and loaded at runtime.

Nick Nieslanik
  • 4,388
  • 23
  • 21
  • Thanks very much for that info Nick. Sorry to be obtuse but I'm not familiar with GACing a DLL (I'm still a novice). Please could you explain this for me? Do you just mean compile the code into a seperate DLL to my MVC project and add it to the bin directory? – Duncan Gravill Oct 17 '11 at 19:47
  • The DLL containing your objects needs to be in the Global Assembly Cache (GAC). A simple bing\google search would lead you to the following: http://en.wikipedia.org/wiki/Global_Assembly_Cache. This wikipedia article should get you started in GACing. – Nick Nieslanik Oct 17 '11 at 19:51
  • Thanks Nick! I tried binging GACing but failed. Thanks for the link. – Duncan Gravill Oct 17 '11 at 20:10
  • GACing DLLs during developement is not ideal... I recommend checking out @Donuts link to [this SO answer](http://stackoverflow.com/a/1891050/1579626) as a much better solution – sǝɯɐſ Oct 14 '14 at 19:37
0

In the end I just switched from testing my app on the VWD development server to testing it on the local iis 7.5 express that is on windows 7. This seems to have been the simplist solution.

Duncan Gravill
  • 4,552
  • 7
  • 34
  • 51