tags:

views:

97

answers:

2

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
+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