如何获得User.Identity在外打工控制器 [英] How to get User.Identity working outside controller

查看:148
本文介绍了如何获得User.Identity在外打工控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的一些自定义的ASP.NET MVC佣工单独的项目

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

在我的一个助手,我需要检查用户身份。

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

在那里工作?

在默认情况下它是生活在 System.Security.Principal 在名为接口接口的IPrincipal

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

推荐答案

本的HtmlHelper具有当前ViewContext并通过HttpContext的,你会得到用户对象为当前用户。在您的扩展方法,你可以用这个

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
}

这篇关于如何获得User.Identity在外打工控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆