示例#1
0
        private async void ByOAuth2()
        {
            MyCredential myCredential = new MyCredential()
            {
                authURL     = Authority,
                ClientId    = ClientId,
                RedirectUri = RedirectUri,
                Resource    = ResourceUri
            };

            Authenticator authenticator = new Authenticator()
            {
                Credential = myCredential
            };
            string accessCode = await authenticator.GetAccessCode();

            if (!string.IsNullOrEmpty(accessCode))
            {
                string json = await authenticator.GetAccessToken(accessCode);

                AccessTokenInfo info = JsonConvert.DeserializeObject <AccessTokenInfo>(json);
                ResponseStr = json.Replace(",\"", ",\r\n\"");
                string[] jwt     = info.AccessToken.Split('.');
                string   payload = jwt.Length == 3 ? jwt[1] : "";
                if (!string.IsNullOrEmpty(payload))
                {
                    JwtPayload jwtPayload = JwtPayload.Base64UrlDeserialize(payload);
                    this.JsonWebTokenStr = jwtPayload.SerializeToJson().Replace(",\"", ",\r\n\"");
                    SetControl(jwtPayload);
                }
            }
        }
示例#2
0
        private async Task <HttpResponseMessage> GetAccessTokenResponse(MyCredential credential, string accessCode)
        {
            string reqUrl = credential.authURL + credential.TokenApi;

            Dictionary <string, string> paramMapping = new Dictionary <string, string>();

            paramMapping["client_id"]    = credential.ClientId;
            paramMapping["redirect_uri"] = credential.RedirectUri;
            paramMapping["resource"]     = credential.Resource;
            paramMapping["grant_type"]   = "authorization_code";
            paramMapping["code"]         = accessCode;
            HttpClient         client = new HttpClient();
            HttpRequestMessage reqMsg = new HttpRequestMessage(HttpMethod.Post, reqUrl);

            reqMsg.Content = new FormUrlEncodedContent(paramMapping);

            HttpResponseMessage response = await client.SendAsync(reqMsg);

            return(response);
        }
示例#3
0
        public static string GetRequestUrl(MyCredential credential, string responseType)
        {
            Dictionary <string, string> paramMapping = new Dictionary <string, string>();

            paramMapping.Add("response_type", responseType);
            paramMapping.Add("client_id", credential.ClientId);
            if (!string.IsNullOrEmpty(credential.Resource))
            {
                paramMapping.Add("resource", credential.Resource);
            }
            paramMapping.Add("redirect_uri", credential.RedirectUri);
            StringBuilder builder  = new StringBuilder(credential.authURL + credential.AuthorizeApi);
            List <string> paraList = new List <string>();

            foreach (var pair in paramMapping)
            {
                paraList.Add(string.Format("{0}={1}", pair.Key, pair.Value));
            }
            builder.Append(string.Join("&", paraList));
            string reqUrl = builder.ToString();

            return(reqUrl);
        }
示例#4
0
        private async Task <HttpResponseMessage> GetAuthorizeResponse(MyCredential credential)
        {
            string reqUrl = GetRequestUrl(credential, "code");

            return(await client.GetAsync(reqUrl));
        }