Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get User.Identity working outside controller

I have separate project with some of my custom ASP.NET MVC helpers

In one of my helpers I need to check user identity.

How may I get User.Identity working there?

By default it is living in System.Security.Principal in interface called interface IPrincipal

like image 374
Joper Avatar asked May 05 '11 13:05

Joper


2 Answers

More easily you may access it by:

HttpContext.Current.User.Identity

So the HttpContext.Current is the trick.

like image 127
Zoka Avatar answered Sep 22 '22 01:09

Zoka


The HtmlHelper has the current ViewContext and via HttpContext you'll get the User object for the current User. In your extension Method you can use this

public static bool MyHelper(HtmlHelper helper)
{
    var userIdentity = helper.ViewContext.HttpContext.User.Identity;
    // more code
}
like image 43
DanielB Avatar answered Sep 22 '22 01:09

DanielB