Xamarin.Android中的Auth0注销问题

时间:2019-03-25 07:17:32

标签: c# xamarin.android auth0

当前在我的Xamairn.Android中,我有2个活动Mainactivity和SignoutActivity。 在我的Mainactivity中,有一个按钮,当用户单击该按钮时,它会转到SignoutActivity。

在这里,我已经实现了Auth0的登出代码。

以下是代码- 在MainAcitivity-

this.Signout.Click += delegate
    {
         using (var intent = new Intent(this, typeof(SignOutActivity)))
            {
                this.StartActivity(intent);
            }
    };

SignOutAcitivity-

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    this.LogoutAsync();
    Finish();

}
public async Task<BrowserResult> LogoutAsync()
{
    var clientOptions = new Auth0.OidcClient.Auth0ClientOptions
    {
        Domain ="",
        ClientId = "",
        Scope = "openid email offline_access",
        Browser = new PlatformWebView()
    };
    var logoutUrl = "Signout URl";
    string redirectUri = "I have proper callback uri";

    Dictionary<string, string> dictionary = new Dictionary<string, string>();
    dictionary.Add("client_id", "random1234");
    dictionary.Add("returnTo", clientOptions.RedirectUri);

    string endSessionUrl = new RequestUrl(logoutUrl).Create(dictionary);

    var logoutRequest = new LogoutRequest();
    BrowserResult browserResult = null;

    browserResult = await clientOptions.Browser.InvokeAsync(new BrowserOptions(endSessionUrl, redirectUri)
    {
        Timeout = TimeSpan.FromSeconds((double)logoutRequest.BrowserTimeout),
        DisplayMode = logoutRequest.BrowserDisplayMode
    });

    return browserResult;
}

Platformview-

class PlatformWebView : IBrowser
{
public Task<BrowserResult> InvokeAsync(BrowserOptions options)
{
    if (string.IsNullOrWhiteSpace(options.StartUrl))
    {
        throw new ArgumentException("Missing StartUrl", nameof(options));
    }

    if (string.IsNullOrWhiteSpace(options.EndUrl))
    {
        throw new ArgumentException("Missing EndUrl", nameof(options));
    }
    var tcs = new TaskCompletionSource<BrowserResult>();

    void Callback(string response)
    {
        ActivityMediator.Instance.ActivityMessageReceived -= Callback;

        // set result
        if (response == "UserCancel")
        {
            tcs.SetResult(new BrowserResult { ResultType = BrowserResultType.UserCancel });
        }
        else
        {
            tcs.SetResult(new BrowserResult
            {
                Response = response,
                ResultType = BrowserResultType.Success
            });
        }
    }
    ActivityMediator.Instance.ActivityMessageReceived += Callback;
    var uri = Android.Net.Uri.Parse(options.StartUrl);
    var intent = new Intent(Intent.ActionView, uri);
    intent.AddFlags(ActivityFlags.NoHistory)
        .AddFlags(ActivityFlags.NewTask);
    Application.Context.StartActivity(intent);
    return tcs.Task;
}
}
}

问题是浏览器永远不会返回到应用程序,另外我在返回browserResult时放置了调试器,该调试器也没有命中。请帮帮我。

0 个答案:

没有答案