【问题标题】:C# - Xamarin.iOs - Add a nonce to xamarin.auth requestC# - Xamarin.iOs - 向 xamarin.auth 请求添加随机数
【发布时间】:2017-04-08 20:25:11
【问题描述】:

我找不到向我的 Xamarin.Auth 请求添加随机数以连接到我的 okta 登录名的方法。我是 xamarin 和 nugets 包的新手,我不知道如何修改 1.3.0 版的 OAuth2Authenticator 的实现。

我正在尝试将请求参数用作:

auth.RequestParameters.Add("nonce", Guid.NewGuid().ToString("N"));

但我一直在 okta 的 nonce 无效错误中运行。

如果你们中有人知道如何解决它。

完整的请求来了:

   //start login flow seting our openId flow
    public void StartFlow(string responseType, string scope)
    {

        //webViewLogin.Hidden = false;
        var auth = new OAuth2Authenticator(
            clientId: OAuthClientId,
            scope: scope,
            authorizeUrl: new Uri(oktaTenantUrl),
            redirectUrl: new Uri(OAuthRedirectUrl)
            );
        auth.RequestParameters.Add("nonce", Guid.NewGuid().ToString("N"));
        auth.Completed += (sender, eventArgs) =>
        {
            DismissViewController(true, null);
            if (eventArgs.IsAuthenticated)
            {
                    // Use eventArgs.Account to do wonderful things
                }
        };
        PresentViewController(auth.GetUI(), true, null);
    }

【问题讨论】:

  • 你得到什么错误?
  • 我从服务器收到此错误 > The+authentication+request+has+an+invalid+nonce+parameter.

标签: c# xamarin xamarin.ios


【解决方案1】:

最终通过修改 OAuth2Authenticator 开始工作,为第一个请求添加一个随机数。

这里是修复:

public override Task<Uri> GetInitialUrlAsync()
    {
        var url = new Uri(string.Format(
            "{0}?client_id={1}&redirect_uri={2}&response_type={3}&scope={4}&state={5}&nonce={6}",
            authorizeUrl.AbsoluteUri,
            Uri.EscapeDataString(clientId),
            Uri.EscapeDataString(this.redirectUrl.AbsoluteUri),
            IsImplicit ? "token" : "code",
            Uri.EscapeDataString(scope),
            Uri.EscapeDataString(requestState),
            Uri.EscapeDataString(nonce)));



        var tcs = new TaskCompletionSource<Uri>();
        tcs.SetResult(url);
        return tcs.Task;
    }

  this.nonce = Guid.NewGuid().ToString("N");

【讨论】:

    【解决方案2】:

    有一种更简单的方法可以实现这一点。看来这是一个错误/没有很好记录的过程。这个issue 解释了该怎么做:

    您可能会覆盖 OnCreatingInitialUrl: https://github.com/xamarin/Xamarin.Auth/blob/ad7f6d6453506ced0ab3af499a7492f13973e3a3/source/Xamarin.Auth.LinkSource/OAuth2Authenticator.cs#L322

    我无法使用 RequestParameters 属性 看到,或者至少在那个版本中没有。

    因此解决方案是创建另一个继承 OAuth2Authenticator 的类并覆盖 OnCreatingInitialUrl:

    using System;
    using System.Collections.Generic;
    using Xamarin.Auth;
    
    namespace Alerte.Health.App.Droid.Classes
    {
        public class MyOAuth2Authenticator : OAuth2Authenticator
        {
            public MyOAuth2Authenticator(string clientId, string scope, Uri authorizeUrl, Uri redirectUrl, GetUsernameAsyncFunc getUsernameAsync = null, bool isUsingNativeUI = false) : base(clientId, scope, authorizeUrl, redirectUrl, getUsernameAsync, isUsingNativeUI)
            {
            }
    
            public MyOAuth2Authenticator(string clientId, string clientSecret, string scope, Uri authorizeUrl, Uri redirectUrl, Uri accessTokenUrl, GetUsernameAsyncFunc getUsernameAsync = null, bool isUsingNativeUI = false) : base(clientId, clientSecret, scope, authorizeUrl, redirectUrl, accessTokenUrl, getUsernameAsync, isUsingNativeUI)
            {
            }
    
            protected override void OnCreatingInitialUrl(IDictionary<string, string> query)
            {
                query.Add("nonce",Guid.NewGuid().ToString("N"));
            }
        }
    }
    

    然后用这个代替OAuth2Authenticator,继续正常流程:

    var auth = new MyOAuth2Authenticator(
        clientId,
        scope,
        new Uri(authorizeUrl),
        new Uri(redirectUrl));
    

    等等等等

    【讨论】:

      【解决方案3】:

      问题在于,如果没有继承 Authenticator,就不可能拦截 ctor 中的调用,并且第一个 HTTP 请求已经在 ctor 中发生。

      当代码到达时:

      auth.RequestParameters.Add("nonce", Guid.NewGuid().ToString("N"));

      已经创建了参数并且已经触发了 HTTP 请求。

      以上两个答案都可以解决问题,但需要继承。

      计划为自定义参数添加另一个字典到ctor,以便及时准备。

      【讨论】:

        猜你喜欢
        • 2014-08-15
        • 2014-01-18
        • 2017-12-01
        • 1970-01-01
        • 2015-01-14
        • 2018-10-24
        • 2013-02-12
        • 2013-06-18
        • 1970-01-01
        相关资源
        最近更新 更多