Ejemplo n.º 1
0
        public Uri BuildAuthorizationUrl(
            IToken token,             
            NameValueCollection additionalParameters)
        {
            if (token.Type == TokenType.Request)
            {                
                OAuthParameters authParameters = new OAuthParameters()
                {
                    Token = token.Token
                };                

                if (additionalParameters != null)
                    authParameters.AdditionalParameters.Add(additionalParameters);

                // Construct final authorization Uri (HTTP method must be GET)
                string query = authParameters.ToQueryStringFormat();

                UriBuilder authUri = new UriBuilder(this.AuthorizationUrl);

                if (String.IsNullOrEmpty(authUri.Query))
                    authUri.Query = query;
                else
                    authUri.Query = authUri.Query.Substring(1) + "&" + query;

                return authUri.Uri;
            }
            else
                throw new ArgumentException("Invalid token type for Authorization");
        }
Ejemplo n.º 2
0
        public Uri BuildEchoCallUri(params KeyValuePair<string, string>[] pairs)
        {
            int timestamp = UnixTime.ToUnixTime(DateTime.Now);

            OAuthParameters parameters = new OAuthParameters();
            parameters.ConsumerKey = ConsumerStore.FixedConsumer.Key;
            parameters.Nonce = new GuidNonceProvider().GenerateNonce(timestamp);
            parameters.SignatureMethod = "HMAC-SHA1";
            parameters.Timestamp = timestamp.ToString(CultureInfo.InvariantCulture);
            parameters.Version = "1.0";
            parameters.Token = TokenGenerator.FixedAccessToken.Token;

            foreach (KeyValuePair<string, string> pair in pairs)
                parameters.AdditionalParameters.Add(pair.Key, pair.Value);

            parameters.Signature = ServiceProviderContext.GetSigningProvider("HMAC-SHA1").ComputeSignature(
                SignatureBase.Create("GET", this.EchoApiBaseUri, parameters),
                ConsumerStore.FixedConsumer.Secret,
                TokenGenerator.FixedAccessToken.Secret);

            UriBuilder builder = new UriBuilder(this.EchoApiBaseUri)
            {
                Query = parameters.ToQueryStringFormat()
            };

            return builder.Uri;
        }
Ejemplo n.º 3
0
        public Uri BuildRequestTokenUri()
        {
            int timestamp = UnixTime.ToUnixTime(DateTime.Now);

            OAuthParameters parameters = new OAuthParameters();
            parameters.ConsumerKey = ConsumerStore.FixedConsumer.Key;
            parameters.Nonce = new GuidNonceProvider().GenerateNonce(timestamp);
            parameters.SignatureMethod = "HMAC-SHA1";
            parameters.Timestamp = timestamp.ToString(CultureInfo.InvariantCulture);
            parameters.Version = "1.0";
            parameters.Callback = "http://yourownsite.com/";

            parameters.Signature = ServiceProviderContext.GetSigningProvider("HMAC-SHA1").ComputeSignature(
                SignatureBase.Create("GET", this.RequestTokenBaseUri, parameters),
                ConsumerStore.FixedConsumer.Secret,
                null);

            UriBuilder builder = new UriBuilder(this.RequestTokenBaseUri) 
            { 
                Query = parameters.ToQueryStringFormat() 
            };
                
            return builder.Uri;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 作者:Vincen
        /// 时间:2013.11.18 AM
        /// 描述:获取MC访问密钥(Token)
        /// </summary>
        /// <param name="type">用户类型(1:教师 2:学员 3:管理员)</param>
        /// <param name="userId">用户编号</param>
        /// <param name="userName">用户名称(一般为英文名)</param>
        /// <returns></returns>
        public static string GetMcToken(string type, string userId, string userName)
        {
            IConsumer fixedConsumer = new OAuthConsumer(WebCommon.Global.McKey, WebCommon.Global.McSecret, "METEN", ConsumerStatus.Valid);

            var timestamp = UnixTime.ToUnixTime(DateTime.Now);
            var uri = new Uri(WebCommon.Global.McAuthenticateUrl);

            var parameters = new OAuthParameters()
            {
                ConsumerKey = fixedConsumer.Key,
                Nonce = new GuidNonceProvider().GenerateNonce(timestamp),
                SignatureMethod = "HMAC-SHA1",
                Timestamp = timestamp.ToString(CultureInfo.InvariantCulture),
                Version = "1.0"
            };

            //--自定义参数
            parameters.AdditionalParameters.Add("user", userId);
            parameters.AdditionalParameters.Add("username", userName);
            switch (type)
            {
                case "1"://教师
                    parameters.AdditionalParameters.Add("teacher", "true");
                    break;
                case "2"://学员
                    break;
                case "3"://管理员
                    parameters.AdditionalParameters.Add("admin", "true");
                    break;
            }
            //--自定义参数

            //parameters.Signature = ServiceProviderContext.GetSigningProvider("HMAC-SHA1").ComputeSignature(SignatureBase.Create("GET", uri, parameters), fixedConsumer.Secret, null);

            var builder = new UriBuilder(uri)
            {
                Query = parameters.ToQueryStringFormat()
            };

            var token = string.Empty;
            var webRequest = WebRequest.Create(builder.Uri) as HttpWebRequest;
            if (null != webRequest)
            {
                webRequest.ServicePoint.Expect100Continue = false;

                using (var stream = webRequest.GetResponse().GetResponseStream())
                {
                    if (null != stream)
                    {
                        using (var responseReader = new StreamReader(stream))
                        {
                            var responseData = responseReader.ReadToEnd();
                            XDocument xdoc = XDocument.Parse(responseData);
                            var query = from n in xdoc.Descendants("token") select n;
                            token = query.First().Value;
                        }
                    }
                }
            }

            return token;
        }