Example #1
0
        static String GetAccessToken(String name, String password)
        {
            try
            {
                // Create a message for the Client Account and Password profile
                // We need to specify the username and password for the client
                // and a POST will be sent with the body:
                // wrap_name=<name>&wrap_password=<password>
                var requestMessage = new ClientAccountRequest(name, password);

                // DEBUG: Show this message on screen
                ShowObject("AccessTokenRequestMessage (Client Account and Password Profile)", requestMessage);

                // Send the message to the Access Token URL of the Authorization Server
                var request = WebRequest.Create(accessTokenUrl);
                request.WriteRequest(requestMessage);

                // DEBUG: Show HTTP request for access token
                ShowRequest("Access Token Request (Client Account and Password Profile)", request as HttpWebRequest);

                // Parse and show result
                var responseMessage = request.GetResponse().CreateAccessTokenResponse();

                // DEBUG: Show token on screen
                ShowObject("AccessTokenResponseMessage (Client Account and Password Profile)", responseMessage);

                return responseMessage.AccessToken;
            }
            catch (WebException exception)
            {
                ShowResponse(
                    "Resource response (exception, Client Account and Password Profile)",
                    exception.Response as HttpWebResponse);
            }
            return null;
        }
Example #2
0
        /// <summary>
        /// Creates a message from a collection of names and values
        /// </summary>
        /// <param name="names">The collection to initialize the request message.</param>
        /// <returns></returns>
        public static AccessTokenRequest CreateAccessTokenRequest(this NameValueCollection names)
        {
            if (null == names)
                throw new ArgumentNullException("names");

            AccessTokenRequest message = null;
            if (false == String.IsNullOrEmpty(names[WrapConstants.Parameters.Assertion]))
                message = new AssertionRequest();
            else if (false == String.IsNullOrEmpty(names[WrapConstants.Parameters.Name]))
                message = new ClientAccountRequest();
            else if (false == String.IsNullOrEmpty(names[WrapConstants.Parameters.UserName]))
                message = new UserNameRequest();
            else if (false == String.IsNullOrEmpty(names[WrapConstants.Parameters.ClientSecret]))
                message = new WebAppRequest();
            else if (false == String.IsNullOrEmpty(names[WrapConstants.Parameters.VerificationCode]))
                message = new RichAppRequest();
            if (null == message)
                throw new WrapMessageException("Message not recognized.");

            foreach (String key in names.Keys)
            {
                String value = names[key];
                if (false == key.StartsWith("wrap_", StringComparison.OrdinalIgnoreCase) || String.IsNullOrEmpty(value))
                    continue;
                message.SetParameter(key, value);
            }

            message.Validate();

            return message;
        }