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!