![](/img/trans.png)
[英]Identityserver4 not redirecting to xamarin.forms android app after login
[英]Azure Authentication with AAD & Google in a Xamarin Forms Project not Redirecting back to app after Authorized
Azure活動目錄
Google+身份驗證
Xamarin Forms,PCL(NuGet 2.4.0.282)
Microsoft.Azure.Mobile.Client 4.0.0和4.0.2
成功登錄后,我的手機不會返回我的應用程序。 我有兩台測試電話和一個模擬器,登錄后它們顯示不同的信息。
電話1(Google身份驗證顯示為灰色,僅保持“正在加載”)
我已經完成了我在Stack OverFlow上找到的所有內容,這很有意義,而且似乎適用於當前版本的NuGets。 這個人似乎和我有類似的問題,但是在登錄后Azure中的 Google Log 無法重定向,請在 此處輸入鏈接描述
我嘗試將代碼集成到我的項目中。 然后我將我的Azure信息輸入Xamarin的示例中: https : //github.com/xamarin/xamarin-forms-samples/tree/master/WebServices/TodoAzureAuth
我得到相同的結果。 我已經嘗試過AAD和Google+ Auth。 登錄后,它僅停留在瀏覽器中。 所以我覺得客戶端代碼必須正確。 但是我在我的Azure服務器代碼上找不到任何混亂。 我已經在具有C#和Node.Js后端的項目中嘗試過此操作。(對於我的一個項目)我允許的外部重定向URL是ToDoList53172://easyauth.callback,並且在AndroidManifest.xml中如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.xamarin.sample.TodoAzure">
<uses-sdk android:minSdkVersion="15" />
<application android:label="TodoAzure" android:icon="@drawable/icon">
<activity android:name="com.microsoft.windowsazure.mobileservices.authentication.RedirectUrlActivity" android:launchMode="singleTop" android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="ToDoList53172" android:host="easyauth.callback" />
</intent-filter>
</activity>
</application>
</manifest>
OLD:我覺得我不應該發布所有其他代碼。 全部都在上面發布的Xamarin示例項目中。 如果人們認為我應該。 新:我添加了更多代碼只是為了幫助人們。 我不想超載,但最好將所有信息放在一個地方。 這是我的MainActivity.cs代碼
using System;
using System.Threading.Tasks;
using Android.App;
using Android.Content.PM;
using Android.OS;
using Microsoft.WindowsAzure.MobileServices;
using Android.Webkit;
namespace TodoAzure.Droid
{
[Activity(Label = "TodoAzure.Droid",
Icon = "@drawable/icon",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
Theme = "@android:style/Theme.Holo.Light")]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity, IAuthenticate
{
MobileServiceUser user;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
App.Init((IAuthenticate)this);
LoadApplication(new App());
}
public async Task<bool> AuthenticateAsync()
{
bool success = false;
try
{
if (user == null)
{
// The authentication provider could also be Facebook, Twitter, or Microsoft
user = await TodoItemManager.DefaultManager.CurrentClient.LoginAsync(this, MobileServiceAuthenticationProvider.Google, Constants.URLScheme);
if (user != null)
{
CreateAndShowDialog(string.Format("You are now logged in - {0}", user.UserId), "Logged in!");
}
}
success = true;
}
catch (Exception ex)
{
CreateAndShowDialog(ex.Message, "Authentication failed");
}
return success;
}
public async Task<bool> LogoutAsync()
{
bool success = false;
try
{
if (user != null)
{
CookieManager.Instance.RemoveAllCookie();
await TodoItemManager.DefaultManager.CurrentClient.LogoutAsync();
CreateAndShowDialog(string.Format("You are now logged out - {0}", user.UserId), "Logged out!");
}
user = null;
success = true;
}
catch (Exception ex)
{
CreateAndShowDialog(ex.Message, "Logout failed");
}
return success;
}
void CreateAndShowDialog(string message, string title)
{
var builder = new AlertDialog.Builder(this);
builder.SetMessage(message);
builder.SetTitle(title);
builder.SetNeutralButton("OK", (sender, args) => { });
builder.Create().Show();
}
}
}
就像我上面說的,我也曾在AAD上嘗試過。 上面的代碼適用於Google。
這是我使用“ https://todolistjbb.azurewebsites.net/.auth/login/aad ”登錄然后訪問“ https://todolistjbb.azurewebsites.net/.auth/me ”后獲得的信息
我覺得我嘗試了很多事情。 我已經記錄了66.68小時的工作,它們只是試圖在我的應用中獲取身份驗證。...請...有人告訴我我做錯了! 我在這里丟了它:'(
根據您的描述,我假設您正在使用Azure App Service身份驗證/授權提供的服務器管理的身份 驗證 。 由於您正在使用Microsoft.Azure.Mobile.Client> = 4.0.0,因此對於您的移動客戶端,您將利用以下代碼段通過服務器流進行日志記錄:
var user = await client.LoginAsync(this, provider, "{url_scheme_of_your_app}");
您可以遵循的詳細信息將身份驗證添加到應用程序 。 此外,您需要將您的應用添加到允許的外部重定向URL 。
根據手機中的錯誤消息2:
todolistjbbservice://easyauth.callback/#authorization_code=xxxxx
看來您沒有正確配置授權重定向URI 。 對於Azure Active Directory提供程序,您可以按照此處注冊Web App / API或本機應用程序。 對於Google提供商,您可以點擊此處 。
在正確配置了首選身份提供者之后,您需要將應用添加到“允許的外部重定向URL”中:
ToDoList53172://easyauth.callback
,然后保存更改。 解決此問題的方法不是以“ URL方案”的大寫字母開頭。 我花了2個多星期才弄清楚。 我認為這篇論文沒有寫在任何地方,但我敢肯定。 所以,要解決此問題,我將“ ToDoList53172”切換為“ todolist53172”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.