locked
Windows authentication in Web API Rest RRS feed

  • Question

  • User741428353 posted

    Hi friends,

    I am facing a serious problem with Windows authentication in Web api. I am not able to get the users identity in Windows auth mode. Here is my method code.

    [AllowAnonymous]
    public class AuthorizeController : ApiController
    {
    [Authorize]
    [HttpGet]
    public string PrimaryIdentityUser()
    {
    string strUserName = String.Empty;

    try
    {
    // strUserName = System.Web.HttpContext.Current.User.Identity.Name;
    strUserName = User.Identity.Name;
    }
    catch (Exception ex)
    {
    throw new FaultException<CommonExceptionFault>(new CommonExceptionFault() { ErrorCode = "-1", ErrorDetails = ex.Message }, ex.Message);
    }
    return strUserName;

    }
    }

    Case 1 : When executing this from local and published, it returns strUserName as  empty.

    Case 2 : When I replace [AllowAnonymous] attribute above class with [Authorize] attribute,  I am getting  exception withreason phrase as unauthorized.

    In short, I am not able to track Users identity in windows authentication

    I am trying to call from console app.

    Can somebody please help me.?

    Thanks in advance

    Monday, July 11, 2016 6:16 AM

Answers

  • User36583972 posted

    Hi Anjeleena,

    As far as I know, in WebApi 2 you can use RequestContext. Principal from within a method on ApiController. The following ways for your reference.

               if (HttpContext.Current != null && HttpContext.Current.User != null
            && HttpContext.Current.User.Identity.Name != null)
                {
                    string  userName = HttpContext.Current.User.Identity.Name;
                    string userId  = RequestContext.Principal.Identity.GetUserId();
                }
                var indenty = this.User.Identity;
    
                WindowsIdentity identity = HttpContext.Current.Request.LogonUserIdentity;

    Case 2 : When I replace [AllowAnonymous] attribute above class with [Authorize] attribute,  I am getting  exception withreason phrase as unauthorized.

    I think you should pass a Windows Authentication credential from client to Web API service.

    How to pass Windows Authentication credential from client to Web API service:

    http://stackoverflow.com/questions/26430045/how-to-pass-windows-authentication-credential-from-client-to-web-api-service

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 12, 2016 5:02 AM

All replies

  • User36583972 posted

    Hi Anjeleena,

    As far as I know, in WebApi 2 you can use RequestContext. Principal from within a method on ApiController. The following ways for your reference.

               if (HttpContext.Current != null && HttpContext.Current.User != null
            && HttpContext.Current.User.Identity.Name != null)
                {
                    string  userName = HttpContext.Current.User.Identity.Name;
                    string userId  = RequestContext.Principal.Identity.GetUserId();
                }
                var indenty = this.User.Identity;
    
                WindowsIdentity identity = HttpContext.Current.Request.LogonUserIdentity;

    Case 2 : When I replace [AllowAnonymous] attribute above class with [Authorize] attribute,  I am getting  exception withreason phrase as unauthorized.

    I think you should pass a Windows Authentication credential from client to Web API service.

    How to pass Windows Authentication credential from client to Web API service:

    http://stackoverflow.com/questions/26430045/how-to-pass-windows-authentication-credential-from-client-to-web-api-service

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 12, 2016 5:02 AM
  • User741428353 posted

    Thanks Yohann.

    I have tried all these :( but still I am getting 'Response status code does not indicate success: 401 (Unauthorized).' from all these.  RequestContext.Principal.Identity.Name and HttpContext.Current.User.Identity.Name

    I am calling web api service from a console app.

    Is there any other configuration settings needed or Will web api work as normal with Widows authorization?

    Can somebody please help me.

    Tuesday, July 12, 2016 1:31 PM
  • User36583972 posted

    Hi  Anjeleena,

    You can try the following code in your console application.

             Uri uri = new Uri("http://myServer/api/values");
                WebClient client = new WebClient();
                //client.Credentials = CredentialCache.DefaultCredentials;
                client.Credentials = new NetworkCredential("username", "password");
                using (Stream data = client.OpenRead(uri))
                {
                    using (StreamReader sr = new StreamReader(data))
                    {
                        string result = sr.ReadToEnd();
                        Console.WriteLine(result);
                    }
                }

    You can also refer the following tutorial.

    WebClient not sending credentials? 

    http://kristofmattei.be/2013/02/20/webclient-not-sending-credentials-heres-why/

    Best Regards,

    Yohann Lu

    Tuesday, July 19, 2016 9:16 AM