コード例 #1
0
        public static string AuthenticateConnection(string baseUrl, string pass)
        {
            var url_matches = new Regex(URL_PATTERN).Match(baseUrl);

            var urlBuilder = new UriBuilder();

            urlBuilder.Scheme = "https";

            string hostName = url_matches.Groups ["host"].Value;

            if (String.IsNullOrWhiteSpace(hostName))
            {
                return("No Host Found");
            }
            urlBuilder.Host = hostName;

            string port = url_matches.Groups ["port"].Value;

            if (!String.IsNullOrEmpty(port))
            {
                urlBuilder.Port = UInt16.Parse(port.Substring(1));
            }

            urlBuilder.Path = "api";

            var client  = new RestClient(urlBuilder.Uri);
            var request = new RestRequest("authenticate", Method.POST);

            // var pass_hash = BitConverter.ToString (Crypto.Hash (pass));
            request.AddJsonBody(new { pass });
            request.Timeout = 5000;

            var response        = client.Execute(request);
            var responseContent = JsonConvert.DeserializeObject
                                  <Dictionary <string, string> > (response.Content);

            string status, token, error;

            if (responseContent != null && responseContent.TryGetValue("success", out status))
            {
                if (Boolean.Parse(status))
                {
                    responseContent.TryGetValue("token", out token);
                    connection.AssignClient(urlBuilder.Uri, token);
                    EntityDatabase.get().SaveAccessToken(token);
                    return(status);
                }
                else
                {
                    responseContent.TryGetValue("error", out error);
                    return(error);
                }
            }

            return(response.Content);
        }