10
Sto scrivendo un HTMLHelper ma ho bisogno di accedere a User.Identity.Name, come faccio?Accesso User.Identity.Name nella classe HTMLHelper
Sto scrivendo un HTMLHelper ma ho bisogno di accedere a User.Identity.Name, come faccio?Accesso User.Identity.Name nella classe HTMLHelper
public static string YourHtmlHelper(this HtmlHelper html)
{
var name = html.ViewContext.HttpContext.User.Identity.Name;
}
Si potrebbe voler controllare e vedere se User.Identity è nullo prima di provare ad afferrare il Nome.
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;
}