你好,我正試圖將Auth0登錄添加到我的Xamarin forms移動應用程式。 我正在按照快速入門的方法進行操作,但遇到了一個問題。我應該在哪里添加這段代碼呢?
var client = new Auth0Client(new Auth0ClientOptions
{
Domain = "******.auth0.com",
ClientId = "******"
}, this)。
我是Xamarin的新手,不知道該在哪里添加它。我試著把它添加到服務檔案夾中的seperact檔案中,但是Auth0.OidcClient.Android只安裝在我的android專案中,我試著在主專案中安裝,但是總是失敗
uj5u.com熱心網友回復:
Auth0.OidcClient.Android最新版本對我不起作用,所以我使用了3.0.1。
因此,我遵循的步驟是
在Android專案中安裝Auth0.OidcClient.Android 3.0.1
。
在主專案中創建一個IAuthService檔案(介面)
公共介面IAuthService
{
Task<LoginResult> Login();
任務 Logout();
}
在Android專案中創建一個AuthenticationService類
。
使用Xamarin.Forms;
using Auth0.OidcClient;
[assembly: Dependency(typeof(AuthenticationService))]。
命名空間XamarinAuth02.Droid
{
公共類AuthenticationService
: IAuthService
{
private Auth0Client _auth0Client;
公共的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()。
回傳結果。
}
public async Task Logout()
{
_ = await _auth0Client.LogoutAsync();
}
}
}
在android專案的主要活動中 LaunchMode = LaunchMode.SingleTask是重要的
。[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)。
}
現在你可以創建一個IAuthService的物件并呼叫登錄/注銷 private readonly
IAuthService _authenticationService。
_authenticationService = DependencyService.Get<IAuthService>()。
private async void Button_Clicked(object sender, EventArgs e)
{
loginresult = await _authenticationService.Login()。
如果(loginresult.IsError)
{
await DisplayAlert("Error", "Login Failed", "Okay");
}
否則
{
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)。
}
對我的打字方式感到抱歉,希望這能幫助到別人
。轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/329865.html
標籤:
下一篇:讀取csv檔案中的列但輸出錯誤
