【问题标题】:Using Microsoft Authentication Library (MSAL) in Unity for SSO in iOS app在 iOS 应用程序中使用 Unity 中的 Microsoft 身份验证库 (MSAL) 进行 SSO
【发布时间】:2021-12-26 14:42:27
【问题描述】:

我们为公司开发了一系列增强现实应用程序。这些应用程序基于游戏引擎 Unity(当前版本为 2020.3.7f1)。现在,我们面临的问题是,我们想在我们的应用程序中实现 SSO 方法,因此我们公司的每个成员都使用他的公司用户凭据对自己进行身份验证。要对用户进行身份验证,我们希望/必须使用 Microsoft 身份平台,因此使用 Microsoft 身份验证库 (MSAL)。

我们已经在 Azure 上创建了一个带有客户端 ID、重定向 uri、授权 uri、端点、租户等的客户端,并且它已通过 Microsoft (https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-v2-ios) 的官方示例项目成功测试。

但是现在我们坚持在 Unity 中建立这个过程。我们创建了 MSAL 库 dll (https://github.com/AzureAD/microsoft-authentication-library-for-dotnet),将其导入 Unity 并使用命名空间 Microsoft.Identity.Client 调用它。 由于安全原因,我们只允许使用 PublicClientApplicationBuilder(这完全有意义),所以我们需要基于 Web 的登录屏幕。问题是我们无法在 Unity 中实现 Web 视图,用户可以在其中登录 Microsoft,我们可以将接收到的令牌从 Web 视图解析到我们的应用程序中以进行进一步的通信。

我们在互联网上进行了大量研究以找到解决方案,但只有一些关于 Unity 中的 MSAL 与 Android 相结合的帖子以及其他参考我之前提到的 git 存储库的帖子。

我们如何在 Unity 中为 iOS 设备使用 MSAL?

示例代码:

 IPublicClientApplication PublicClientApp = PublicClientApplicationBuilder
        .Create(kClientID)
        .WithRedirectUri(kRedirectUri)
        .WithAuthority(new Uri(kAuthority))
        .Build();
 IEnumerable<string> scopes = new List<string> { "https://graph.microsoft.com/.default" };
 AuthenticationResult result;
 result = await PublicClientApp.AcquireTokenInteractive(scopes)
                   .ExecuteAsync();

 result = await PublicClientApp
        .AcquireTokenInteractive(scopes)
        .WithParentActivityOrWindow(new object)
        .ExecuteAsync();

 Debug.Log(result.AccessToken);

【问题讨论】:

    标签: ios unity3d single-sign-on openid-connect msal


    【解决方案1】:

    移动设备的标准解决方案是 AppAuth 模式,它涉及使用集成系统浏览器来让用户登录。请参阅 this code example article 了解其外观,以便您了解所需的行为。

    所以看起来他们与 iOS 的关键是避免通过 webviews 登录并告诉 MSAL 使用 ASWebAuthenticationSession 窗口 - this Microsoft article 解释了如何。

    【讨论】:

      【解决方案2】:

      我们使用以下资产做到了这一点: UnitySafariViewController

      它在 Unity 之外打开 Safari ViewController 并支持包含授权码的回调。收到授权码后,其他所有事情(请求令牌等)都可以通过 Unity WebRequest 完成。 请记住将 iOS 上代理的 redirect uri 添加到 Info.plist 中。

      最重要的链接: The auth flow

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-02
        • 1970-01-01
        • 1970-01-01
        • 2021-08-03
        • 1970-01-01
        • 2020-05-14
        • 1970-01-01
        相关资源
        最近更新 更多