Xamarin.Auth或IdentityModel + IdentityServer4

时间:2019-03-05 11:00:35

标签: authentication xamarin xamarin.forms xamarin.ios identityserver4

有人使用Xamarin.Auth或IdentityModel实现了IdentityServer4身份验证吗? 我已经尝试过使用IdentityModel和Xamarin.Auth,但它们的外观都缺少功能。

  1. 我似乎找不到在Xamarin.Auth的OAuth2Authenticator中设置response_type和response_mode的方法。 但是我喜欢如何打开本机浏览器并收听其完成/错误事件。对于未经授权的客户端,在这种情况下显然会出错,因为.Net Core中的IdentityServer4需要response_type和response_mode。

  2. 我似乎找不到使用IdentityModel与本机浏览器进行交互的方法,我已经读到推荐的方法是使用外部/本地浏览器而不是Webview。除此之外,使用Webview尝试通过google进行身份验证时,我收到disallowed_useragent错误。

1 个答案:

答案 0 :(得分:0)

已解决。我最终使用Xamarin.Auth与IdentityServer4一起使用(至少在iOS中至少如此),因为Google身份验证(而非android)对将WebView与IdentityModel一起使用不满意。

对于可能与OAuth2Authenticator和IdentityServer4一起使用时遇到类似问题的其他开发人员的注释,以下是发生的事情和对我有用的事情:

  1. 由于我们在服务器中具有默认值,因此在IdentityServer4中不需要提供response_type和response_mode。

  2. 我的主要问题是Xamarin.Auth请求令牌时出现“ client_unauthorized”错误,因为RedirectUrl在末尾加了斜杠,而配置的RedirectUrl没有。

    • 初始化OAuth2Authenticator时,我的RedirectUrl末尾没有斜杠
    • 当我在调用OneListAppContext.AuthenticationState.Authenticator.OnPageLoading(url)之前在AppDelegate.cs的OpenUrl方法内将NSUrl转换为Uri时,神奇地添加了末尾的额外斜杠。
    • 我想这是url查询中问号前加斜杠的标准吗?如果是这样,我不知道,因为在URL中添加?name = value一直有效。
    • 来自Safari的OpenUrl方法中的NSUrl是myappname:// oauth?code = 1ec450f88737fcae0a2786699d5df24e3ca97a8b55c09da2ef13d58035821134 ...
    • 将NSUrl转换为Uri之后的新Uri是myappname:// oauth /?code = 1ec450f88737fcae0a2786699d5df24e3ca97a8b55c09da2ef13d58035821134 ...(请注意,在oauth之后/)

修复非常简单: 1.更改为我的客户端配置的RedirectUrl,使其末尾带有/(例如myappname:// oauth /) 2.在客户端中初始化RedirectUrl,以使OAuth2Authentic结尾于/(例如myappname:// oauth /)