无法访问OnActionExecuting下的User.Identity(ActionExecutingContext filterContext)

时间:2012-11-05 23:31:36

标签: c# .net asp.net-mvc authorization authorize-attribute

我不知道我错过了什么,但我真的不能参考  User.Identity下的OnActionExecuting(ActionExecutingContext filterContext)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.Routing;

.....

    public class RealUserAttribute : ActionFilterAttribute
    {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                 /// if (User.Identity.IsAuthenticated)

有任何线索如何解决?

3 个答案:

答案 0 :(得分:11)

我找到了解决方案。

应该是

filterContext.HttpContext.User.Identity.IsAuthenticated

答案 1 :(得分:9)

我相信你应该使用AuthorizeAtribute而不是ActionFilter。尝试这样的事情:

using System.Web;
using System.Web.Mvc;

public class AuthorizeUser : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        var id = filterContext.RequestContext.HttpContext.User.Identity;
    }
}

答案 2 :(得分:1)

curl --include \
 --request GET \
 https://cdn.contentful.com/spaces/$SPACE_ID/environments/master/entries?access_token=$TOKEN&links_to_entry=7fYxxN67JKyUiYuE4Cq8sI&content_type=post

有效!无需检查ID User或User.Identity为空