![](/img/trans.png)
[英]What are diffs between iOS App (Xamarin) vs. iOS XAML App (Xamarin.Forms) vs. Cross-Platform
[英]Authentication with Amazon Cognito User Pools in Xamarin.Forms Cross-Platform App
我正在使用 Xamarin.Forms 開發跨平台移動應用程序。 我有一個 Amazon Cognito 用戶池,可以通過第三方(Facebook、Google)以及本機應用程序用戶身份驗證(用戶名/密碼)進行身份聯合,其中包含我的所有用戶。 我目前有一個帶有單個重定向 URL、 https://localhost:PORT
我已經編寫了與 Cognito 接口的 .NET 標准幫助程序庫。 我有一個接口ICognitoAuthHelper
,其中包含一個方法AuthenticateUserAsync(PoolRequest request)
。 PoolRequest
object 有一個AuthorizationCode
屬性,用於身份驗證代碼授權流程。 AuthenticateUserAsync
function 實質上將授權代碼發布到 Cognito 應用程序客戶端 TOKEN 端點以接收 JWT,然后我讓實用程序庫對其進行解碼以檢索有關經過身份驗證的用戶身份的聲明。
輸入問題:我想利用我編寫的這個實用程序庫在我的 Xamarin.Forms 應用程序中進行身份驗證。
這是一個簡單的用例,以及我將如何(和做)在 ASP.NET Core MVC web 應用程序中執行身份驗證流程:
1)我點擊“使用 Facebook 登錄”按鈕重定向到 Cognito 應用程序客戶端的 AUTHORIZE 端點
2)當應用程序客戶端重定向回我的重定向 URL ( localhost
) 時,我從 URL 中獲取code
查詢參數,將其包裝在PoolRequest
中,並將其提供給我的實用程序庫。 然后,如上所述,庫將向 TOKEN 端點發布授權代碼( code
),以開始對返回的 JWT 的解碼過程。
3)實用程序庫的響應將包含用戶聲明,我可以重定向到配置文件頁面,設置 session 變量等。
如何通過 Xamarin.Forms 應用程序實現這種身份驗證流程? 我曾考慮使用WebView
,但不確定如何捕獲將從 Cognito 應用程序客戶端返回的身份驗證代碼。 我在應用程序客戶端上為 Xamarin 設置什么重定向 URL? 解碼令牌集后,如何使WebView
導航到配置文件頁面?
非常感謝您對此的任何幫助。
我認為已經解決了這個問題。 我沒有 Mac,所以我只能在 Android 上進行測試,但解決方案需要做幾件事:
myapp://login
public ICommand FacebookLogin => new Command(FacebookLoginCommand);
public ICommand GoogleLogin => new Command(GoogleLoginCommand);
private async void FacebookLoginCommand()
{
var redirect = "<COGNITO APP CLIENT AUTHORIZE URL (FB)>"; //
await Launcher.OpenAsync(redirect);
}
private async void GoogleLoginCommand()
{
var redirect = "<COGNITO APP CLIENT AUTHORIZE URL (Google)>"; //
await Launcher.OpenAsync(redirect);
}
請注意,這里我在redirect
變量中引用自定義方案重定向 URL,如上所述。
MainActivity
上放置一個IntentFilter
。[IntentFilter(
new [] { Intent.ActionView },
DataScheme = "myapp",
DataHost = "login",
AutoVerify = true,
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable})]
public class MainActivity : global::Xamarin.Forms.Android.FormsAppCompatActivity
{
...
}
重寫MainActivity
中的OnNewIntent(Intent intent)
方法,獲取授權碼查詢參數。 它可以用intent.Data?.GetQueryParameter("code")
提取。
使用我的實用程序庫來交換令牌/用戶身份的代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.