Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access to User.Identity under OnActionExecuting(ActionExecutingContext filterContext)

I am not sure what do I am missing but I really cannot refer to User.Identity under 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)

Any clue how to fix it?

like image 329
Friend Avatar asked Nov 05 '12 23:11

Friend


3 Answers

I found the solution.

It should be

filterContext.HttpContext.User.Identity.IsAuthenticated
like image 116
Friend Avatar answered Jan 05 '23 01:01

Friend


I believe that you should be using AuthorizeAtribute instead of ActionFilter. Try something like this:

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

public class AuthorizeUser : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        var id = filterContext.RequestContext.HttpContext.User.Identity;
    }
}
like image 31
Miloš Ostojić Avatar answered Jan 04 '23 23:01

Miloš Ostojić


filterContext.RequestContext.HttpContext.Request.IsAuthenticated

works! and no need to check id User or User.Identity are null

like image 26
Lapenkov Vladimir Avatar answered Jan 04 '23 23:01

Lapenkov Vladimir