asp.net-mvc – ASP.NET MVC在自定义操作过滤器中查找当前用户名

我正在为asp.net MVC创建一个自定义动作过滤器。

在OnActionExecuting()方法中。

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    string userName =  ?????// how can I get this?
}

我需要找出当前的用户名(我正在使用表单身份验证)

在控制器中,我可以简单地做User.Identity.Name

有没有办法在ActionFilter中获取用户名?

解决方法

string userName = filterContext.HttpContext.User.Identity.Name;

如果你想检查是否有一个经过身份验证的用户:

string userName = null;
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
    userName = filterContext.HttpContext.User.Identity.Name;
}

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....