I'm using [WebAPI Basic Authentication Authorization Filter] to authorize users on consuming my simple web api data service.
Question is: everything work on localhost but when I publish my service to the web (http://www.mywebsite.com) client app always gets "Unauthorize" response.
This line is all I change (on client side) when switching from localhost to the web
//client.BaseAddress = new Uri("http://localhost:11992/"); // on localhost work
client.BaseAddress = new Uri("http://mywebsite.com/"); // returns 401 Unauthorized
Tried with adding machine key using remote IIS manager but same thing happens.
reference this machine key in system.web (web.config)
and authentication mode on IIS is as follows
Still doesnt work. Obviously I'm missing something here.
UPDATE I'm extending basic auth. filter which I later apply on my controller action (the one I access from client side)
[MyBasicAuthenticationFilter]
public class DataController : RavenController { ... }
and inside this custom auth. filter there is hardcoded username and pass
public class MyBasicAuthenticationFilter : BasicAuthenticationFilter
{
public MyBasicAuthenticationFilter()
{ }
public MyBasicAuthenticationFilter(bool active)
: base(active)
{ }
protected override bool OnAuthorizeUser(string username, string password, HttpActionContext actionContext)
{
if (username == "myuser" && password == "mypass")
return true;
else
return false;
}
}
and from client side (wpf client)
client.BaseAddress = new Uri("http://mywebsite.com/");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "myuser", "mypass"))));
I'm confused because same code work fine on localhost but when I publish code and change this client.BaseAddress to my actual website url it returns 401 error.
ParseAuthorizationHeader
method of theBasicAuthenticationFilter
class. My best guess on the possible cause right now is that the Default Encoding on the server you have deployed to is not the same as your local dev. – Adrian Sanguineti