Compile error in string userData overload

Jun 16, 2011 at 3:32 PM


There seems to be a problem with the simpler syntax overload in your second code sample:

new FormsAuthentication().SetAuthCookie(user.UserId, true, "arbitrary string here");

This gives a compile error:

Member 'System.Web.Security.FormsAuthentication.SetAuthCookie(string, bool, string)' cannot be accessed with an instance reference; qualify it with a type name instead

I believe it's because the method signature clashes with FormsAuthentication's own SetAuthCookie:

// FormsAuthentication
SetAuthCookie(string userName, bool createPersistentCookie, string strCookiePath)

// FormsAuthenticationExtensions
SetAuthCookie(string userName, bool createPersistentCookie, string userData, DateTime? expiresOn = null)

I was able to workaround this by passing a null expiresOn, even though it's optional:

new FormsAuthentication().SetAuthCookie(user.UserId, true, "arbitrary string here", null /* HACK */);
But it would be great if the last parameter could be omitted.

Anyway, I appreciate the effort you put into this incredibly useful little gem.

Daniel Liuzzi