c# - Call web api with basic authentication always get 401 unauthorized from IIS -


tonight come search how call web api hosted in iis.

everything work well in local visual studio iis express. strangely, after publish on iis server. 401 unauthorized :'(

here code use , settings iis server. grateful if can me. thank you

**

the controller , function try call (with basic authentication attribute)

**

    [httpget]     [actionname("get_userid")]     [identitybasicauthentication]     [authorize]     public httpresponsemessage get_userid(string username)     {         httpresponsemessage res = new httpresponsemessage(httpstatuscode.created);         try         {             var user = membership.getuser(username, false);             if (user != null)             {                 res = request.createresponse(httpstatuscode.ok, (guid)user.provideruserkey);             }             else             {                 res = request.createresponse(httpstatuscode.expectationfailed);                 res.content = new stringcontent("error");                 res.reasonphrase = "username not find in database";             }         }         catch (exception exc)         {             //set response message exception             res = request.createresponse(httpstatuscode.internalservererror);             res.content = new stringcontent("exception");             res.reasonphrase = exc.message;         }         return res;     } 

**

client side - how call web api , build httpclient

**

    public static async task<httpresponsemessage> requeststart(string requesturl, string webapiurlbase = globals.webapi_url, bool isauthenticatememberrequest = false)     {         if (webapiurlbase == null)         {             webapiurlbase = globals.webapi_url;         }         var response = new httpresponsemessage(httpstatuscode.created);          using (var client = new httpclient())         {             if (isauthenticatememberrequest)             {                 string strtoencode = applicationdata.current.localsettings.values["username"].tostring() + ":" + applicationdata.current.localsettings.values["password"].tostring();                 var authenticationbytes = encoding.ascii.getbytes(strtoencode);                  client.defaultrequestheaders.authorization = new authenticationheadervalue("basic",                 convert.tobase64string(authenticationbytes));             }             client.baseaddress = new uri(globals.webapi_url);             client.defaultrequestheaders.accept.clear();             client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json"));             response = await client.getasync(requesturl);         }          return response;     } 

**

iis configuration (apppool => networkservices - integrate)

** enter image description here

**

fiddler debug

** enter image description here

finally after search many times , many houres myself. find solution. should never enable basic authentication.... know it's weird ^^ if want use custom basic authentication. disabled basic authentication on iis , goes well.


Comments