ASP.NET Web API Self-Host with Windows Authentication

前端 未结 9 1220
不知归路
不知归路 2021-01-30 11:31

I am trying to use the ASP.NET Web API Self-Host option with Windows authentication so I can determine the logged on user and ultimately accept or reject the user based on their

相关标签:
9条回答
  • 2021-01-30 12:13

    Here is a link to a short video explaining how to use authorization.

    http://www.asp.net/web-api/videos/getting-started/authorization

    In essence use the [Authorize] attribute on the class, catch the error and return an HTTP 401 response and then have the client detect it and go to the login page

    0 讨论(0)
  • 2021-01-30 12:13

    Just to add, if you're using tpeczek's solution and also using HttpClient, you might need to do this:

            var handler = new HttpClientHandler();
            handler.UseDefaultCredentials = true;
            _httpClient = new HttpClient(handler);
    
    0 讨论(0)
  • 2021-01-30 12:20

    i have hosted "Web API" in windows service and this is what i did to support windows authentication (basically based on above question, answers, some related articles - i am just consolidating as it may be helpful for others)

    @HTTP Server (web api):

    Set (reference: http://msdn.microsoft.com/en-us/library/system.web.http.selfhost.httpselfhostconfiguration.clientcredentialtype(v=vs.118).aspx),

    HttpSelfHostConfiguration.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
    

    @Client:

    And then as Allan mentioned (above) set UseDefaultCredentials to true.

    Using HttpClient:

    var handler = new HttpClientHandler();
        handler.UseDefaultCredentials = true;
        _httpClient = new HttpClient(handler);
    

    Using WebClient (reference: http://msdn.microsoft.com/en-us/library/system.net.webclient.usedefaultcredentials.aspx )

    set webclient's usedefaultcrednetials to 'true'.

    Best Regards!

    0 讨论(0)
提交回复
热议问题