Certainly a lot more involved than what I have ever done. This is the very basic approach when I save a User/Password in configuration so things work on machines not attached to the domain (mainly because my test environment is often not on the domain of the Portal but the Portal uses Windows Auth)
private async Task<TokenCredential> GetWindowsCredential()
{
Uri sharingUri = new Uri($"{Settings.PortalUrl}/sharing/rest");
TokenCredential credential;
try
{
credential = await AuthenticationManager.Current.GenerateCredentialAsync(sharingUri, "", "");
}
catch (Exception)
{
credential = await AuthenticationManager.Current.GenerateCredentialAsync(sharingUri, Settings.WindowsUser, Settings.WindowsPwd);
}
return credential;
}
I do the same in another app (it is WPF not Xamarin though) that uses a form to get user input. All done basically the same except for a little ditty to get the password:
string password = new NetworkCredential(string.Empty, _securePassword).Password;
In WPF when the user enters into a password text box it gives you a SecureString type. I don't know if Xamarin is the same, but that's a way I figured to get a plain text string from the SecureString object.
Also there are some things that don't directly ask for Credential or have as a parameter but it is required. The following does the trick for me in those cases
AuthenticationManager.Current.AddCredential(await GetWindowsCredential());
I have never had issue with this.
As a general rule I will call into GetWindowsCredential anytime I need a credential unless there is a significant performance concern and I am sure that I am using the saved Credential over a short time. But the token does expire and so if cached too long will no longer be valid
Hope that helps
Thanks,
-Joe