Пример #1
0
        /// <summary>
        /// Create implicit flow url.
        /// </summary>
        /// <param name="clientId">The client id.</param>
        /// <param name="scopes">The scopes for this request.</param>
        /// <param name="redirectUri">The redirect url.</param>
        /// <param name="state">The state.</param>
        /// <param name="nonce">The nonce.</param>
        /// <param name="loginHint">The login hint.</param>
        /// <param name="acrValues">The acr values (idp:Google b c).</param>
        /// <param name="additionalValues">Additional values.</param>
        /// <returns>The implicit flow URL.</returns>
        public string CreateImplicitFlowUrl(
            string clientId,
            string[] scopes    = null,
            string redirectUri = null,
            string state       = "random_state",
            string nonce       = "random_nonce",
            string loginHint   = null,
            string acrValues   = null,
            Dictionary <string, string> additionalValues = null)
        {
            // Create a request.
            var request = new OAuth2Client(new Uri(_baseAddress.TrimEnd('/') + "/" + _authorizeEndpoint.Trim('/')));

            return(request.CreateImplicitFlowUrl(clientId, (scopes == null ? null : String.Join(" ", scopes)),
                                                 redirectUri, state, nonce, loginHint, acrValues, additionalValues));
        }
        public async static Task <AuthorizeResponse> DoImplicitFlowAsync(Uri endpoint, string clientId, string scope, Uri callbackUri)
        {
            var client   = new OAuth2Client(endpoint);
            var startUri = client.CreateImplicitFlowUrl(
                clientId,
                scope,
                callbackUri.AbsoluteUri);

            try
            {
                var result = await WebAuthenticationBroker.AuthenticateAsync(
                    WebAuthenticationOptions.None,
                    new Uri(startUri));

                if (result.ResponseStatus == WebAuthenticationStatus.Success)
                {
                    return(new AuthorizeResponse(result.ResponseData));
                }
                else if (result.ResponseStatus == WebAuthenticationStatus.UserCancel)
                {
                    throw new Exception("User cancelled authentication");
                }
                else if (result.ResponseStatus == WebAuthenticationStatus.ErrorHttp)
                {
                    throw new Exception("HTTP Error returned by AuthenticateAsync() : " + result.ResponseErrorDetail.ToString());
                }
                else
                {
                    throw new Exception("Error returned by AuthenticateAsync() : " + result.ResponseStatus.ToString());
                }
            }
            catch
            {
                // Bad Parameter, SSL/TLS Errors and Network Unavailable errors are to be handled here.
                throw;
            }
        }