I am writing an HTMLHelper but I need to access User.Identity.Name, how do I do that?
+2
A:
public static string YourHtmlHelper(this HtmlHelper html)
{
var name = html.ViewContext.HttpContext.User.Identity.Name;
}
eu-ge-ne
2009-06-24 09:48:59
+2
A:
you might want to check and see if User.Identity is null first before trying to grab the Name.
public static string YourHtmlHelper(this HtmlHelper html)
{
var identity = html.ViewContext.HttpContext.User.Identity;
if (identity != null)
{
return html.ViewContext.HttpContext.User.Identity.Name;
}
return string.Empty;
}
Mike Geise
2009-06-24 18:03:42