Question

I implemented a custom cookie for Forms Authorization on my website:

http://msdn.microsoft.com/en-us/library/system.web.security.formsauthenticationticket.aspx

Since I am storing some additional information userData :

Account account = accountRepository.GetAccount(model.Email);
string userData = string.Format("{0}|{1}", account.UserId, account.AccountTypeId);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                                                                model.Email,
                                                                DateTime.Now,
                                                                DateTime.Now.AddMinutes(48),
                                                                model.RememberMe,
                                                                userData,
                                                                FormsAuthentication.FormsCookiePath);

I am thinking about creating a Static Helper Class to extract the userData from my cookie.

However I cannot acccess the User.Identity.Name outside my controller.

How can I access it oustide my controller? And is it wrong to be a static class?

Thanks

No correct solution

OTHER TIPS

My approach was creating a Static Helper class as initially planned :

public static class CookieHelper
{
    public static Guid GetCurrentUserId(IIdentity identity)
    {
        Guid result;

        var userData = ((FormsIdentity)identity).Ticket.UserData;

        result = new Guid(userData.Split('|')[0]);

        return result;

    }

    public static int GetCurrentAccountTypeId(IIdentity identity)
    {
        int result;

        var userData = ((FormsIdentity)identity).Ticket.UserData;

        result = Convert.ToInt16((userData.Split('|')[1]));

        return result;

    }
}

Inside my controller I call it this way :

Guid currentUserId = CookieHelper.GetCurrentUserId(User.Identity);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top