How to get User.Identity working outside controller

别等时光非礼了梦想. 提交于 2020-07-08 03:56:48

问题


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


回答1:


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
}



回答2:


More easily you may access it by:

HttpContext.Current.User.Identity

So the HttpContext.Current is the trick.



来源:https://stackoverflow.com/questions/5898170/how-to-get-user-identity-working-outside-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!