.NET MAUI 7 Android WebViewClient OnReceivedHttpAuthRequest override is never called - c#

In my .NET MAUI App I' ve implemented a WebView with a url. When navigate to the website a simple js popup login come up. Now in my app i want to set credentials programmatically.
Im trying to handle js login request on webpage by overriding OnReceivedHttpAuthRequest method and pass username and password to the handler.Proceed but the method is never called.
My class:
public class BasicAuthWebViewClient : WebViewClient
{
private readonly string Username;
private readonly string Password;
public BasicAuthWebViewClient(string username, string password)
{
Username = username;
Password = password;
}
public override void OnReceivedHttpAuthRequest(Android.Webkit.WebView view, HttpAuthHandler handler, string host, string realm)
{
handler.Proceed(Username, Password);
}
}
And the constructor:
androidWebView.SetWebViewClient(new BasicAuthWebViewClient(settings.Credentials.Username, settings.Credentials.Password));

For now I resolved passing username and password to the url like this:
https://username:password#mywebsite.com/

Related

What's the pattern for sharing a RestAPI token?

Is there a pattern for how should I store and reuse a restAPI authorisation token across multiple classes?
I'm consuming a RestAPI, my login code takes a user, password and server and then returns an auth token. That login code sits in a common base class for all my RestAPI calls which works fine. But each new object doesn't know the token so has to reauthorise. I need them to share the same token once it's been generated. I can't use a singleton as I may have to login with multiple different users in the same session.
I'm sure there's a pattern for that but I can't find it, can you help?
What you need is a cache. The login service can be a singleton that cache access tokens, you can use a concurrent dictionary to implement the access tokens cache.
Something like this:
public class LoginService
{
private ConcurrentDictionary<string, string> accessTokenCache = new ConcurrentDictionary<string, string>();
private string callServerLogin(string user, string password)
{
throw new NotImplementedException();
}
public string Login(string user, string password)
{
var accessToken = callServerLogin(user, password);
accessTokenCache[user] = accessToken;
return accessToken;
}
public bool TryGetCachedAccessToken(string user, out string accessToken )
{
return this.accessTokenCache.TryGetValue(user, out accessToken);
}
}

how to maintain wcf service per session calling url from android studio

I created WCF service and calling it from the android studio and its working fine, but after implementing WCF perSession functionality it is working for a single user at a time.
Problem:
My problem is when i am hitting WCF url with multiple user my sessionID get overwrite by the latest logged in user, So how to maintain session for multiple user like we do in web application.
I used this to creste session in WCF:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
and this is my method to create sessionid within services class
public class MyService : IMyService
{
static string currentSessionID = string.Empty;
public string createSession()
{
currentSessionID = DateTime.Now.Ticks.ToString();
return currentSessionID;
}
public string Login()
{
var mysessionId = createSession();
return mysessionId;
}
public string Mymethods(string data)
{
string response = "";
if(data.StartsWith("01"))
response = Login();
return response;
}
}
and am hitting this createSession() method only in login function.
Please help me out of this.....
Thanks in advnance.

Default user implementation in Xamarin Forms to auto login

I have created my first Xamarin.Forms application using Xamarin Studio. My application is not able to remember the default username, so that user has to login app every time which is irritating. How can I store the last user credentials to auto login, if he/she has not logged out to from the app? Is it possible in PCL?
Method 1:
Use SettingsPlugin to store the user credentials as you required. This will help to access settings from PCL across all of your mobile apps.
public static class Settings
{
private static ISettings AppSettings
{
get
{
return CrossSettings.Current;
}
}
//Setting Constants
const string UserName = "username";
private static readonly string UserNameDefault = string.Empty;
public static string UserName
{
get { return AppSettings.GetValueOrDefault<string>(UserName, UserNameDefault); }
set { AppSettings.AddOrUpdateValue<string>(UserName, value); }
}
const string Password = "password";
private static readonly string PasswordDefault = string.Empty;
public static string Password
{
get { return AppSettings.GetValueOrDefault<string>(Password, PasswordDefault); }
set { AppSettings.AddOrUpdateValue<string>(Password, value); }
}
}
For autologin create a blank page as splash screen page. Set it to as :
Application.Current.MainPage = new MySplashScreen();
On the behind code of this page :
public partial class MySplashScreen : ContentPage
{
public MySplashScreen()
{
InitializeComponent();
CheckForAutoLogin();
}
private async void CheckForAutoLogin()
{
if (Settings.UserName != string.Empty && Settings.Password != string.Empty)
{
//Redirect to you desired page
}
else
{
//Redirect to login page.
}
}
}
So in above implementation, I have created a temporary page as splash screen just to check for login credential and accordingly redirect to the desired page.
So if you use this Plugin then it is not recommended, to store password like values in string format. For that you need to make use of any encryption/decryption algorithm and then store the encrypted value using Settings.Plugin.
Method 2:
You can use Xamarin.Auth (Cross-Platform SDK). It is a secure way to store your credential. It is a cross-platform SDK for authenticating users and storing their accounts. It can be used to securely store Account objects in an account store so that applications do not always have to re-authenticate users.

How to implement ASP Identity IPasswordStore if I don't have hashed password stored

So I have a MVC5 project with ASP Identity, I don't have direct access to user database instead I was provided with web services, for example:
bool register (string username, string password, string email) //password is plain text
bool login (string username, string password)
My problem is that I have no idea how to implement IUserPasswordStore because ASP Identity force me to use Hash while I'm not allowed to store hashed password because this web services will be used not just by me.
My UserStore.cs:
public Task<string> GetPasswordHashAsync(TUser user)
{
//I'm suppose to provide hashed password here, but since I don't have any hashed password stored, I have no idea how to implement this method
string passwordHash = userTable.GetPassword(user.Id);
return Task.FromResult<string>(passwordHash);
}
public Task<bool> HasPasswordAsync(TUser user)
{
var hasPassword = !string.IsNullOrEmpty(userTable.GetPassword(user.Id));
return Task.FromResult<bool>(Boolean.Parse(hasPassword.ToString()));
}
public Task SetPasswordHashAsync(TUser user, string passwordHash)
{
//do nothing since I don't need to hash the password
return Task.FromResult<Object>(null);
}
Well your application doesn't have a password store, don't implement this.
Override the PasswordSignInAsync method in the SignInManager, and have it call your web service.

passing username and password to soap webservice

I am working on a soap webservice in c#. It look like this:
public class MyService : System.Web.Services.WebService
{
public MyService()
{
}
[WebMethod]
public string Hello()
{
return "hello";
}
}
and I added a service reference to this web service from another website, so I can access the Hello() method from there using the code:
MyServiceSoapClient client = new MyServiceSoapClient();
client.Hello();
Now I need to pass the credentials to that web service. I have tried:
MyServiceSoapClient client = new MyServiceSoapClient();
client.ClientCredentials.UserName.UserName = "test";
client.ClientCredentials.UserName.Password = "pwd";
client.Hello();
But I could not manage to get these credentials in the webservice ( in the Hello() method).
How can I get these values in the webservice?
You get the user via the WebService.User property. This will give you the username but there is no way to retrieve the passed password, this is by design as the authentication happens at the IIS level before your WebService is run.
public class MyService : System.Web.Services.WebService
{
public MyService()
{
}
[WebMethod]
public string Hello()
{
return "hello, my name is " + User.Identity.Name;
}
}

Categories