【问题标题】:Using Identity Server inside an Xamarin app在 Xamarin 应用程序中使用 Identity Server
【发布时间】:2019-07-03 14:13:33
【问题描述】:

我正在尝试让 Xamarin 应用程序与身份服务器一起使用。我已按照以下步骤操作:

1) 下载:https://github.com/IdentityModel/IdentityModel.OidcClient.Samples/tree/master/XamarinForms 2) 从第 1 点运行 Xamarin Forms 应用程序。它按预期工作,即我可以以 Bob 身份登录,并且我通过了面向公众的演示应用程序的身份验证:https://demo.identityserver.io

它按预期工作到这里。

3) 然后我在这里下载面向公众的演示应用程序:https://github.com/IdentityServer/IdentityServer4.Demo。将其添加到 Xamarin 解决方案中。 4)在溶液中;进行查找和替换 - 从 https://demo.identityserver.iohttp://localhost:24997/(这是我的身份服务器在解决方案中的 URL)。

5) 注释掉以下代码行,因为我没有使用 Azure:

//Startup.cs
 //.AddGoogle("Google", options =>
                //{
                //    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;

                //    options.ClientId = Configuration["Secret:GoogleClientId"];
                //    options.ClientSecret = Configuration["Secret:GoogleClientSecret"];
                //})
                //.AddOpenIdConnect("aad", "Sign-in with Azure AD", options =>
                //{
                //    options.Authority = "https://login.microsoftonline.com/common";
                //    options.ClientId = "https://leastprivilegelabs.onmicrosoft.com/38196330-e766-4051-ad10-14596c7e97d3";

                //    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                //    options.SignOutScheme = IdentityServerConstants.SignoutScheme;

                //    options.ResponseType = "id_token";
                //    options.CallbackPath = "/signin-aad";
                //    options.SignedOutCallbackPath = "/signout-callback-aad";
                //    options.RemoteSignOutPath = "/signout-aad";

                //    options.TokenValidationParameters = new TokenValidationParameters
                //    {
                //        ValidateIssuer = false,
                //        ValidAudience = "165b99fd-195f-4d93-a111-3e679246e6a9",

                //        NameClaimType = "name",
                //        RoleClaimType = "role"
                //    };
                //})

//Program.cs
//.ConfigureAppConfiguration((ctx, builder) =>
//{
//    var config = builder.Build();
//    var tokenProvider = new AzureServiceTokenProvider();
//    var kvClient = new KeyVaultClient((authority, resource, scope) => tokenProvider.KeyVaultTokenCallback(authority, resource, scope));
//    builder.AddAzureKeyVault(config["KeyVault:BaseUrl"], kvClient, new DefaultKeyVaultSecretManager());
//})

6) 添加选项。RequireHttpsMetadata = false;到 IdentityServer.Startup。

然后我运行 Xamarin 应用程序和身份服务器。我在 Xamarin 应用程序内的以下行中看到错误(第一行:MainPage.Login_Clicked):

_result = await _client.LoginAsync(new LoginRequest());

错误是:Error connecting to http://localhost:24997/.well-known/openid-configuration

有什么明显的我做错了吗?

【问题讨论】:

  • 1) 消息中还有更多内容吗?该代码表明可能存在相关的原因短语。 2) 如果您自己在浏览器中打开失败的 URL,会发生什么?
  • 1) 内部异常说套接字异常 2) 我可以看到网页。会不会是 xamarin 设备看不到 localhost(身份服务器)。也许我需要浏览到 IP 地址/域名而不是 localhost?
  • 是的,这听起来很可能是原因。我相信你知道,localhost 在两个不同的设备上意味着不同的东西。
  • 试过了,没有任何区别。再次按照我的指示,得到了相同的结果。您是否尝试按照我的指示进行操作。你得到了什么结果?再次感谢。
  • 我无法按照您的说明进行操作,因为我现在没有 Xamarin 设置。等我有时间玩的时候看看能不能试试。

标签: c# xamarin xamarin.forms xamarin.android identityserver4


【解决方案1】:

如果您想使用多个设备进行测试,则不能使用 localhost。您必须将两台设备都放在同一个网络中,并改用您的本地 IP 地址。

对于 IdSrv 应用,您需要正确配置托管 URL,例如如果您在本地开发,请更改launchSettings.json,或设置ASPNETCORE_URL 环境变量。当您运行服务器应用程序时,它应该告诉您它正在侦听 http://192.168.1.101:5000 或任何本地 IP 地址和所选端口。

然后,您需要将其配置为 Xamarin 应用程序中的授权 URL。然后,应用程序可以到达身份服务器并与之正确通信。

如果身份验证不起作用,则来自身份服务器的服务器日志应为您提供有关问题所在的更多详细信息。这样,您可以调整身份服务器中的客户端设置,以及 Xamarin 应用程序中的身份验证设置,直到一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 2016-08-03
    • 2019-12-09
    • 1970-01-01
    相关资源
    最近更新 更多