private void btnAuth_Click(object sender, EventArgs e)
        {
            if (authuser.Text.Length == 0)
            {
                MessageBox.Show("Please enter a username!");
                return;
            }

            var myCallback = new SezameAuthCallbackType(authCallback);

            _manager.auth(authuser.Text, "Dotnet example UI", myCallback);
        }
Пример #2
0
        public async Task auth(string username, string message, SezameAuthCallbackType callback)
        {
            var webRequestHandler = new WebRequestHandler();

            webRequestHandler.ClientCertificates.Add(certificate); // X509Certificate
            var invoker = new SezameAuthenticationServiceInvoker(webRequestHandler, true);

            SezameResult response = null;
            var          timeout  = TimeSpan.FromSeconds(60);

            response = await invoker.RequestAuthenticationAsync(username, message, "auth", (int)Math.Ceiling(timeout.TotalMinutes));

            var status = response.GetParameter(SezameResultKey.AuthenticationStatus);

            if (status == "notlinked")
            {
                callback(SezameAuthenticationResultKey.NotPaired);
                return;
            }

            var authId = response.GetParameter(SezameResultKey.Id);

            var result = SezameAuthenticationResultKey.Timedout;

            if (status == "initiated")
            {
                result = await Task.Run <SezameAuthenticationResultKey>(async() =>
                {
                    int sleeptime     = 1000;
                    int loopPassCount = (int)Math.Ceiling(Math.Ceiling((double)timeout.TotalMilliseconds) / sleeptime);
                    while (loopPassCount > 0)
                    {
                        response = await invoker.CheckAuthenticationStatusAsync(authId);
                        status   = response.GetParameter(SezameResultKey.AuthenticationStatus);
                        if (status == "authorized")
                        {
                            return(SezameAuthenticationResultKey.Authenticated);
                        }
                        else if (status == "denied")
                        {
                            return(SezameAuthenticationResultKey.Denied);
                        }
                        loopPassCount--;
                        System.Threading.Thread.Sleep(sleeptime);
                    }
                    return(SezameAuthenticationResultKey.Timedout);
                });
            }

            callback(result);
        }