Always HttpContext.Current.User.Identity.IsAuthenticated = false in MVC4 with windows authentication mode and not working correctly

bharath picture bharath · Jul 7, 2014 · Viewed 7.6k times · Source

I am trying to do customAuthorization in MVC when the authentication mode is "windows"

    public override void OnAuthorization(AuthorizationContext filterContext)
    {

        #region if
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        { .......}
     }

But always IsAuthenticated is returning false. I don't know how to invoke windows authentication before the onAuthorization(..).

But when i do like this:

public override void OnAuthorization(AuthorizationContext filterContext)
    {

        #region if
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        { .......}else{
         **base.OnAuthorization(filterContext);**
         }
     }

The onAuthorization() method is hitting repeatedly because of the base.onAuthorization() and in the second attempt IsAuthenticated is returning true.

May i know the reason for this behavior?

When i googled results came up saying that IsAuthenticated will be true in Forms Authentication mode but not in Windows mode. As mentioned in the below link

http://www.anujvarma.com/windows-ad-authentication-and-the-authorize-attributelogin-popup/

Please Help me.

Answer

bharath picture bharath · Jul 8, 2014

The issue is solved. I disabled allowanonymous property in IIS server settings and it started working as expected i.e the user is being authenticated before hitting onauthorization(..).