コード例 #1
0
        public static async Task <string> CRMAuthenticate(string username, string password, string domain)
        {
            string userId;
            string WebApiurl = AppSettings.GetByKey("CRMAPI");

            using (var client = new HttpClient(new HttpClientHandler()
            {
                Credentials = new NetworkCredential(username, password, domain)
            }))
            {
                List <Systemuser> list = new List <Systemuser>();
                // New code:
                try
                {
                    client.BaseAddress = new Uri(WebApiurl);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
                    client.DefaultRequestHeaders.Add("OData-Version", "4.0");

                    HttpResponseMessage retrieveResponse = await client.GetAsync("systemusers" + string.Format("?$filter=contains({0},\'{1}\')", "domainname", domain + "\\" + username));

                    if (retrieveResponse.IsSuccessStatusCode)
                    {
                        var Content = await retrieveResponse.Content.ReadAsStringAsync();

                        ODataResponse <Systemuser> itemres = JsonConvert.DeserializeObject <ODataResponse <Systemuser> >(Content);
                        list = Authentication.CollectionFromResponseSet(itemres.Value);

                        userId = list[0].Systemuserid.Value.ToString();
                    }
                    else if (retrieveResponse.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        userId = string.Empty;
                    }
                    else
                    {
                        userId = string.Empty;
                    }
                }
                catch (Exception ex)
                {
                    userId = string.Empty;
                }
            }

            return(userId);
        }