Issue
Hello I am trying to add Auth0 Login to my Xamarin forms Mobile Application, I was following the Quick start and And came into a problem. where do i add this code
var client = new Auth0Client(new Auth0ClientOptions
{
Domain = "******.auth0.com",
ClientId = "******"
}, this);
I am new to Xamarin and cannot figure out where to add it. I tried to add it in a seperact file in Service Folder But Auth0.OidcClient.Android is only installed to my android Project,i Tried to install on main project but it alwasys fails
Solution
Auth0.OidcClient.Android latest version did not work for me So i used 3.0.1
So the steps i followed
Install Auth0.OidcClient.Android 3.0.1 in Android project
In Mainproject create a IAuthService file(Interface)
public interface IAuthService
{
Task<LoginResult> Login();
Task Logout();
}
Create a AuthenticationService class in Android project
Implement IAuthService,Making a Depandancy injunction
using Xamarin.Forms;
using Auth0.OidcClient;
[assembly: Dependency(typeof(AuthenticationService))]
namespace XamarinAuth02.Droid
{
public class AuthenticationService
: IAuthService
{
private Auth0Client _auth0Client;
public AuthenticationService()
{
_auth0Client = new Auth0Client(new Auth0ClientOptions
{
Domain = AuthConfig.Domain,
ClientId = AuthConfig.ClientId
});
}
public async Task<LoginResult> Login(new
{ audience = AuthConfig.Audience })
{
LoginResult result = await _auth0Client.LoginAsync();
return result;
}
public async Task Logout()
{
_ = await _auth0Client.LogoutAsync();
}
}
}
In Main Activity in android project LaunchMode = LaunchMode.SingleTask is importent
[Activity(LaunchMode = LaunchMode.SingleTask, Label = "XamarinAuth02", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
[IntentFilter(
new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "com.companyname.xamarinauth02",//package name
DataHost = "Domain",
DataPathPrefix = "/android/com.companyname.xamarinauth02ThisMyPackageName/callback")]
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
ActivityMediator.Instance.Send(intent.DataString);
}
Now u can create a obj of IAuthService and call login / logout private readonly
IAuthService _authenticationService;
_authenticationService = DependencyService.Get<IAuthService>();
private async void Button_Clicked(object sender, EventArgs e)
{
loginresult = await _authenticationService.Login();
if (loginresult.IsError)
{
await DisplayAlert("Error", "Login Failed", "Okay");
}
else
{
await SecureStorage.SetAsync("accessToken", loginresult.AccessToken);
await SecureStorage.SetAsync("UserName", loginresult.User.FindFirst(c => c.Type == "name")?.Value);
IsLoggedIn = true;
await Navigation.PushAsync(new HomePage(loginresult.User.FindFirst(c => c.Type == "name")?.Value));
}
}
Sorry For my Typing Hope this helps someone
Answered By - Adarsh s
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.