mardi 30 avril 2019

User.Identity.Name returns empty and IIS asks for credentials when API is hosted

User.Identity.Name is empty when hosted on IIS. Also IIS asks for cendentials when IP address is put in URL rather than localhost. Please advise how to fix this.

Windows and Anonymous Authentication is enabled in IIS.

I tried enabling and disabling anonymous authentication in IIS

namespace System.Web.Mvc
{
    public class AccessController : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var isAuthorized = base.AuthorizeCore(httpContext);
            if(!isAuthorized)
            {
                return false;
            }

            return base.AuthorizeCore(httpContext);
        }
    }
}        


My controller
        [System.Web.Http.HttpGet]
        [AccessController]
        [System.Web.Http.Route("api/GetUser")]
        public string GetCurrenTUser()
        {
            if(User.Identity.IsAuthenticated)
            {
                return User.Identity.Name.Replace("EMEA\\", "");
            }

            return "false";

        }

Web.Config

 <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
    <authentication mode="Windows"/>
  </system.web>




Aucun commentaire:

Enregistrer un commentaire