Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在操作筛选器中获取用户ID(不是Identity.Name)_Asp.net_Asp.net Mvc_Asp.net Identity - Fatal编程技术网

Asp.net 在操作筛选器中获取用户ID(不是Identity.Name)

Asp.net 在操作筛选器中获取用户ID(不是Identity.Name),asp.net,asp.net-mvc,asp.net-identity,Asp.net,Asp.net Mvc,Asp.net Identity,我需要在ASP.Net MVC应用程序的操作筛选器中查找UserID。我可以在一个正常的控制器中得到相同的结果,如下所示 我知道如何在行动过滤器中获取用户名 但当我试图获取UserID时,我得到了一个错误 string userID = filterContext.HttpContext.User.Identity.GetUserID(); 错误CS1061“IIdentity”不包含“GetUserID”的定义,并且找不到接受类型为“IIdentity”的第一个参数的扩展方法“GetUs

我需要在ASP.Net MVC应用程序的操作筛选器中查找
UserID
。我可以在一个正常的控制器中得到相同的结果,如下所示

我知道如何在行动过滤器中获取用户名

但当我试图获取UserID时,我得到了一个错误

string userID = filterContext.HttpContext.User.Identity.GetUserID();
错误CS1061“IIdentity”不包含“GetUserID”的定义,并且找不到接受类型为“IIdentity”的第一个参数的扩展方法“GetUserID”

如何在操作筛选器中获取用户ID

更新-参考:

  • 操作过滤器代码

    using System;
    using System.Collections.Generic;////Not used
    using System.Linq;////Not used
    using System.Web;////Not used
    using System.Web.Mvc;
    using System.Web.Routing;
    using Microsoft.AspNet.Identity; ////Not used
    
    namespace MyPortal
    {
        public class ActionFilterSessionCheck : ActionFilterAttribute, IActionFilter
        {
            void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
            {
                string userName = null;
                if (filterContext.HttpContext.User.Identity.IsAuthenticated)
                {
                    userName = filterContext.HttpContext.User.Identity.Name;
    
                    //ERROR***
                    //string userID = filterContext.HttpContext.User.Identity.GetUserID();
    
                }
    
                string sessionLogin = "Login" + userName;
                if (userName == null || (System.Web.HttpContext.Current.Session[sessionLogin] == null))
                {
    
                    if (filterContext.HttpContext.Request.IsAjaxRequest())
                    {
                        //AJAX request
                        throw new Exception("Session time out");
                    }
                    else
                    {
                        // Standard request
                        filterContext.Result = new RedirectToRouteResult(
                                           new RouteValueDictionary
                                           {
                                           { "action", "Login" },
                                           { "controller", "Account" }
                                           });
                    }
                }
    
    
                //Call standard behavior
                this.OnActionExecuting(filterContext);
            }
        }
    
    }
    

    您需要的函数是
    GetUserId
    ,而不是
    GetUserId
    。它是被定义的


    工作示例与错误之间的区别在于,您错误地使用了
    ID
    ,而不是
    ID

    ,即使在过滤器中添加该代码也会出现错误,如果您需要ID,您需要按名称从db手动检查