C#-Xamarin.iOs-向xamarin.auth请求添加随机数

Ant*_*rps 1 c# xamarin.ios xamarin

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

我正在尝试将request参数用作:

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);
    }
Run Code Online (Sandbox Code Playgroud)

mar*_*icz 5

有一种更简单的方法可以实现此目的。看来这是一个错误/没有充分记录的过程。此问题说明了怎么办:

您可能会覆盖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"));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,使用它代替OAuth2Authenticator,然后继续正常过程:

var auth = new MyOAuth2Authenticator(
    clientId,
    scope,
    new Uri(authorizeUrl),
    new Uri(redirectUrl));
Run Code Online (Sandbox Code Playgroud)

等等等