2009-06-24 40 views

回答

17
public static string YourHtmlHelper(this HtmlHelper html) 
{ 
    var name = html.ViewContext.HttpContext.User.Identity.Name; 
} 
5

您可能想要先检查User.Identity是否为null,然后再试图获取名称。

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; 
    }