Getting the username set in the UserData of the FormsAuthenticationTicket form

I am thinking about getting the usernames of my site using this in my opinion (Razor syntax):

@MySite.Helpers.Utils.UserName

heres utils class:

public class Utils
{
    static FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;

    public static string UserName { get { return id.Ticket.UserData; } }
}

Are there any potential issues with this code?

The reason I'm doing this is because Im going to store the username in the userdata field of the new FormsAuthenticationTicket when the user logs in.

I treat it like this because I use facebook connect and want to save the ID there in the username field in db and their names / full names in a separate table.

facebook -. Im , , userdata .

, , : @MySite.Helpers.Utils.UserName

? ?

? ?

+3
1

, : Im userdata FormsAuthenticationTicket, .

cookie . UserData. Razor, :

@User.Identity.Name

, , [Authorize], , , NullReferenceException .

:

public static MvcHtmlString Username(this HtmlHelper htmlHelper)
{
    var identity = htmlHelper.ViewContext.HttpContext.User.Identity;
    if (identity.IsAuthenticated)
    {
        return MvcHtmlString.Create(identity.Name);
    }
    return MvcHtmlString.Empty;
}

:

@Html.Username()
+1

Source: https://habr.com/ru/post/1781838/


All Articles