I'm trying to make a Login for my Xamarin.Forms App and the Windows Authentication does not work for me, but just from my Xamarin-Code. When I try to browse the Webservice through Postman or just a regular Browser for example I return my result. Before Windows Authentication I used Basic Authentication what worked well and uncomplicated for me.
I have a IIS Server 8.5 where my Webservice runs from.
public async Task<bool> GetLoginAccess(UserCredential userCredentials)
{
HttpClientHandler authHandler = new HttpClientHandler()
{
PreAuthenticate = true,
AllowAutoRedirect = true,
UseDefaultCredentials = true
};
using (HttpClient client = new HttpClient(authHandler))
{
_client.BaseAddress = new Uri(Properties.Resources.URL_Webservice_Login);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var response = await _client.GetAsync("api/LoginChecker?application=***&user=***").ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
var access = JsonConvert.DeserializeObject<bool>(json);
return access;
}
else
{
return false;
}
}
}
Error:
{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Date: Tue, 14 Jun 2022 08:21:30 GMT
Server: Microsoft-IIS/8.5
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Android-Received-Millis: 1655194890132
X-Android-Response-Source: NETWORK 401
X-Android-Selected-Protocol: http/1.1
X-Android-Sent-Millis: 1655194890044
X-Powered-By: ASP.NET
Content-Length: 1344
Content-Type: text/html
}}
I don't know what I'm making wrong...
EDIT
So now my Code looks like this:
var credentialsCache = new NetworkCredential(userCredentials.User, userCredentials.Password, "DOM");
HttpClientHandler authHandler = new HttpClientHandler()
{
PreAuthenticate = true,
Credentials = credentialsCache,
AllowAutoRedirect = true,
UseDefaultCredentials = false
};
using (HttpClient client = new HttpClient(authHandler, true))
{
client.BaseAddress = new Uri(Properties.Resources.URL_Webservice_Login);
var response = await _client.GetAsync("/api/LoginChecker?application=***&user=***");
if (response.IsSuccessStatusCode)...
And still not working. If im routing to this Page with the Simulator or another physical android device, i get to enter my Credentials, which works fine there, so something in my Code is wrong... :/
EDIT #2
After hours and hours of failures finding my mistake I decided to try WebClient instead of HttpClient. And guess what: IT WORKS GREAT!
From the IIS documentation:
I'm not an expert in IIS-topics, neither in NTLM, but I guess in order to do so, you'd have to be logged in as a domain user on the respective device. If you are, the device sends the hashed user credentials to IIS, which in turn authenticates you. Since you are logged in on your machine, your browser and postman are able to send this hashed credentials, get authenticated and are allowed to access whatever you have to access. You're not logged in with your user credentials on the mobile device, hence it is not able to send the credentials and therefor you're neither authenticated, nor authorized, which results in the
401 Unauthorized. Even if the app runs in the simulator on your local machine, it does not know about the credentials, same outcome.This answer suggests that you'd have to set the
HttpClientHandler.Credentialsto aCredentialsCacheinstance (code snipped shamelessly copied) in order to make the authentication/authorization work