简体   繁体   English

Xamarin.forms 的 Azure AD B2C“Safari 无法打开页面,因为地址无效”?

[英]Azure AD B2C for Xamarin.forms "Safari cannot open the page because the address is invalid"?

Following along with the Xamarin native sample , the sample itself seems to work fine on ios, but when I put in my own credentials, after entering user name and password I an popup that says "Safari cannot open the page because the address is invalid".跟随 Xamarin 本机示例,示例本身似乎在 ios 上运行良好,但是当我输入自己的凭据时,在输入用户名和密码后,我会弹出一个显示“Safari 无法打开页面,因为地址无效”的弹出窗口. This is my main problem, if anybody can advise that would be very grateful.这是我的主要问题,如果有人可以提出建议,将不胜感激。

The bigger issue though is I don't understand what is actually going on in the code.更大的问题是我不明白代码中实际发生了什么。 I'm not looking for a redirect after authentication anyway.无论如何,我不是在寻找身份验证后的重定向。 I'm just trying to get a token back to my app's code, so I would like to dismiss the webform and return control to my code.我只是想将令牌返回给我的应用程序代码,因此我想关闭网络表单并将控制权返回给我的代码。 But instead I am stuck with the popup.但相反,我被弹出窗口困住了。 (This all works fine and as expected in the sample, but when I put in my own credentials, as best as I can figure them out based on the notes in the sample, I get this error.) (这一切都很好,并且与示例中的预期一致,但是当我输入自己的凭据时,我会根据示例中的注释尽我所能弄清楚它们,我收到此错误。)

I'm thinking it must have to do something with the scopes or redirect settings on the Tenant, but its all very opaque on the page and in the documentation what any of this means Eg what is a "scope?"我认为它必须对租户上的范围或重定向设置做一些事情,但它在页面上和文档中都非常不透明,这意味着什么,例如什么是“范围”? in the first place, and why is the value as the scope argument in thhe sample a url?首先,为什么该值作为 url 示例中的范围参数?

From what I can tell, what I want the scope to be is "OpenId" since I am using local authentication, but if you set "scopes" to the { "OpenId" } you get an error saying those scopes are already included.据我所知,我想要的范围是“OpenId”,因为我使用的是本地身份验证,但是如果您将“范围”设置为 {“OpenId”},您会收到一条错误消息,指出这些范围已经包含在内。 But if you leave it blank, you also get an error saying scopes are required.但是如果您将其留空,您还会收到一条错误消息,指出需要作用域。 So what up with that?那怎么办?

With respect to tenant setting page, if I am only building something to service logins on my app, why do I need to answer "yes" to the question "Include web app/web api?".关于租户设置页面,如果我只是在我的应用程序上构建一些服务登录的东西,为什么我需要对问题“包括 web 应用程序/web api?”回答“是”。 Why is the reply url set to non-existent value " https://myapi "?为什么回复 url 设置为不存在的值“ https://myapi ”? Why is the App ID Uri set to " https://[applicationName]/onmicrosoft.com/demoapi ? and what is its purpose?为什么 App ID Uri 设置为“ https://[applicationName]/onmicrosoft.com/demoapi ?它的目的是什么?

In the "Native Client" section, the registration provides pre-populated fields "Redirect Uri" and "custom redirect uri".在“本机客户端”部分,注册提供了预先填充的字段“重定向 Uri”和“自定义重定向 uri”。 The Redirect URI looks like "urn:ietf:wg:oauth:2.0:oob".重定向 URI 看起来像“urn:ietf:wg:oauth:2.0:oob”。 What is that?那是什么? the pop up says it's a弹出窗口说这是一个

"Unique identifier which B2C will redirect the user agent in an Oath2.0 response" “B2C 将在 Oath2.0 响应中重定向用户代理的唯一标识符”

but it doesnt look like there are enough letters here to possibly be unique ID.但看起来这里没有足够的字母可能是唯一的 ID。

The there is a thing called "the "custom" redirect uri, which I guess is different than the plain old "Redirect URI" and that one looks like有一种叫做“自定义”重定向 uri 的东西,我猜它与普通的旧“重定向 URI”不同,它看起来像

msal3b4c7038-694a-42d6-bab0-43d5b1f86106://auth msal3b4c7038-694a-42d6-bab0-43d5b1f86106://auth

So why is one "custom" and why is the other apprarently not custom?那么为什么一个是“习惯”,而另一个显然不是习惯呢?

Sorry for all the questions but I can't find good documentation explaining any of this.很抱歉所有问题,但我找不到解释这些问题的好文档。 Much appreciated, and if anyone from Azure is listening, maybe they could point to some Xamarin/Azure documentation that would help me understand this.非常感谢,如果 Azure 中的任何人在听,也许他们可以指出一些有助于我理解这一点的 Xamarin/Azure 文档。

Many thanks!非常感谢!

PS Maybe this post has too many questions, and not sure if I am supposed to split these up into separate questions. PS 也许这篇文章的问题太多了,我不确定我是否应该将它们分成单独的问题。 If so, please let me know.如果是这样,请告诉我。 My main question is the first one.我的主要问题是第一个问题。

Relevant Code below:相关代码如下:

    public static string Tenant = "crowdwisdom.onmicrosoft.com";
        public static string ClientID = "3b4c7038-694a-44c6-bab0-43d5b1f86106";
        public static string PolicySignUpSignIn = "B2C_1_susi";
        public static string PolicyEditProfile = "B2C_1_edit_profile";
        public static string PolicyResetPassword = "B2C_1_reset";

        public static string[] Scopes = { "https://crowdwisdom.onmicrosoft.com/demoapi/demo.read" };
        public static string ApiEndpoint = "https://crowdwisdom.azurewebsites.net";

        public static string AuthorityBase = $"https://login.microsoftonline.com/tfp/{Tenant}/";
        public static string Authority = $"{AuthorityBase}{PolicySignUpSignIn}";
        public static string AuthorityEditProfile = $"{AuthorityBase}{PolicyEditProfile}";
        public static string AuthorityPasswordReset = $"{AuthorityBase}{PolicyResetPassword}";

        public static UIParent UiParent = null;

... ...

async void OnSignInSignOut(object sender, EventArgs e)
    {
        try
        {
            if (btnSignInSignOut.Text == "Sign in")
            {
                AuthenticationResult ar = await App.PCA.AcquireTokenAsync(App.Scopes, GetUserByPolicy(App.PCA.Users, App.PolicySignUpSignIn), App.UiParent);
                UpdateUserInfo(ar);
                UpdateSignInState(true);
            }
            else
            {
                foreach (var user in App.PCA.Users)
                {
                    App.PCA.Remove(user);
                }
                UpdateSignInState(false);
            }
        }
        catch(Exception ex)
        {
            // Checking the exception message 
            // should ONLY be done for B2C
            // reset and not any other error.
            if (ex.Message.Contains("AADB2C90118"))
                OnPasswordReset();
            // Alert if any exception excludig user cancelling sign-in dialog
            else if (((ex as MsalException)?.ErrorCode != "authentication_canceled"))
                await DisplayAlert($"Exception:", ex.ToString(), "Dismiss");
        }
    }

Here's the Azure B2C tenant settings:这是 Azure B2C 租户设置:

在此处输入图片说明

I'll break down my answer into two posts since there are two different sets of questions here. 由于这里有两组不同的问题,因此我将答案分为两篇。

Redirect URI's ( how to return to your app ) 重定向URI(如何返回到您的应用)

Leaving the browser and returning to your app is mostly done using the redirect uri. 离开浏览器并返回到您的应用程序大部分是使用重定向uri完成的。 The MSALxxxxxx://oauth is the redirect uri path that you (according to your screenshot) have registered with B2C. MSALxxxxxx:// oauth是您(根据您的屏幕截图)已向B2C注册的重定向uri路径。 That looks good. 这看起来不错。 When you're making the call to Azure AD B2C, you need to make sure the redirect URI is set to that value. 调用Azure AD B2C时,需要确保将重定向URI设置为该值。 When Azure AD B2C generates a token, it will send it forward to that redirect URI. 当Azure AD B2C生成令牌时,它将把令牌转发给该重定向URI。

Now, the app needs to receive that token. 现在,应用程序需要接收该令牌。 This is done by listening to a uri scheme, which is everything before the colon. 这是通过听一个uri方案来完成的,这是冒号之前的一切。 In this case, the URI scheme is MSALxxxxxx. 在这种情况下,URI方案为MSALxxxxxx。 To configure your xamarin app to listen to that URI scheme, follow step 6 in this sample . 要将xamarin应用程序配置为侦听该URI方案,请遵循此示例中的步骤6。 That steps helps you configure the return URI for each kind of platform. 该步骤可帮助您为每种平台配置返回URI。 Now, whenever the browser detects a redirect to MSALxxxx, it will send the message forward to your app. 现在,每当浏览器检测到重定向到MSALxxxx时,它将把消息转发到您的应用程序。 In this case, you will receive a B2C token in your message. 在这种情况下,您会在邮件中收到B2C令牌。

By the way, "urn:ietf:wg:oauth:2.0:oob" is just a default redirect URI that we provide. 顺便说一句,“ urn:ietf:wg:oauth:2.0:oob”只是我们提供的默认重定向URI。 We recommend you only use it for testing purposes, and that you create a unique redirect URI scheme for your app. 我们建议您仅将其用于测试目的,并为应用程序创建唯一的重定向URI方案。

See my other answer above or below that answers the question regarding redirect URI's. 请参阅上方或下方的其他答案,该答案回答有关重定向URI的问题。

Scopes: 作用域:

Scopes are mostly necessary when you're calling an API. 在调用API时,范围是最必要的。 Think of them like permissions. 将它们视为权限。 For example, you could have a "tasks list" api that stores your tasks for the day. 例如,您可能有一个“任务列表” api,用于存储当天的任务。 Suppose you want your app to be able to "read" from the API. 假设您希望您的应用程序能够从API“读取”。 So you call Azure AD B2C, and request an access token that gives you the ability to "read" from the API. 因此,您调用Azure AD B2C,并请求一个访问令牌,该令牌使您能够从API“读取”。 You would do this by inserting "read" as one of the scopes in your request. 为此,您可以在请求中插入“ read”作为作用域之一。

The problem is, any API can have a read scope, so to distinguish which API you are referring to, you use the App ID URI. 问题是,任何API都可以具有读取范围,因此,使用App ID URI来区分您所指的是哪个API。 So in Azure AD B2C, you set the App ID URI as " https://tenantName.onmicrosoft.com/tasksList ". 因此,在Azure AD B2C中,将应用程序ID URI设置为“ https://tenantName.onmicrosoft.com/tasksList ”。 Then when you're making the request, you specify the scope as " https://tenantName.onmicrosoft.com/tasksList/read " (notice that 'read' was added to the end of that "URL") 然后,在发出请求时,将范围指定为“ https://tenantName.onmicrosoft.com/tasksList/read ”(注意,“ read”已添加到该“ URL”的末尾)

As neither of the answers helped me solved this I'll put my answer here as the libraries have been updated since this post.由于这两个答案都没有帮助我解决这个问题,我将把我的答案放在这里,因为自这篇文章以来库已经更新。

The new way to add a redirect URI to the Azure AD B2C Application is like so:向 Azure AD B2C 应用程序添加重定向 URI 的新方法如下所示:

在此处输入图片说明

But I can't find instructions on how to configure the app correctly to make this work.但我找不到有关如何正确配置应用程序以使其工作的说明。 So I had to configure the redirect in the old way like so:所以我不得不像这样以旧方式配置重定向:

在此处输入图片说明

This stopped the error in Safari which I worked out from adding logging to the Microsoft.Identity library, copying the URL it was trying to load from the logs, and pasting it into Safari.这阻止了 Safari 中的错误,我通过向 Microsoft.Identity 库添加日志记录、复制它试图从日志加载的 URL 并将其粘贴到 Safari 中解决了该错误。 This showed that the error from Azure was a missing redirect.这表明来自 Azure 的错误是缺少重定向。

在此处输入图片说明

I hope this helps someone else and if you know why the new way is not working please post it here too.我希望这对其他人有所帮助,如果您知道为什么新方法不起作用,也请在此处发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Azure AD B2C时出现错误“Safari无法打开页面,因为地址无效” - Error “Safari cannot open the page because the address is invalid” with Azure AD B2C Azure AD B2C 上 Xamarin.Forms 抛出错误 B2C '权限' Uri 应该至少有 3 个段 - Azure AD B2C on Xamarin.Forms throwing error B2C 'authority' Uri should have at least 3 segments Azure AD B2C在Xamarin.Forms iOS应用中指定多个身份提供程序 - Azure AD B2C Specify multiple Identity Providers in Xamarin.Forms iOS app 如何使用Azure AD B2C身份验证开发多用户Xamarin.Forms应用 - How to develop a multi-user Xamarin.Forms app with Azure AD B2C authentication 401 在 Xamarin.Forms 上未经 Azure b2c 授权 - 401 Unauthorized with Azure b2c on Xamarin.Forms Azure AD B2C永远不会登录Xamarin Forms - Azure AD B2C never logs in on Xamarin Forms 更改Xamarin形式的Azure AD B2C密码的方法 - Method to Change Password for Azure AD B2C in Xamarin Forms Xamarin Azure AD B2C登录,卡在登录页面 - Xamarin Azure AD B2C login, stuck at login page 我们如何删除/自定义 Xamarin 表单中的 Azure AD B2C 身份验证 webview 页面标题栏? - How can we remove / customize Azure AD B2C authentication webview page title bar in Xamarin forms? 在 Xamarin.Forms 中使用 MSAL 获取 Azure B2C SignUpSignIn 应用程序声明 - Getting Azure B2C SignUpSignIn Application Claims using MSAL in Xamarin.Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM