Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace FormsLdap
- {
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.IO;
- using System.Net;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- public class WebApiService
- {
- public TResult CreateUsersPostRequest<TResult>(CreateUsersModel model, string accessToken)
- {
- string url = ConfigurationManager.AppSettings[AppConfigKeys.CreateUsersUrl];
- const string contentType = @"application/json";
- var request = (HttpWebRequest)WebRequest.Create(url);
- request.ContentType = contentType;
- request.Method = "POST";
- request.Headers.Add("Authorization", $"Bearer {accessToken}");
- using (var writer = new StreamWriter(request.GetRequestStream()))
- {
- string json = JsonConvert.SerializeObject(model);
- writer.Write(json);
- }
- var response = (HttpWebResponse)request.GetResponse();
- using (var streamReader = new StreamReader(response.GetResponseStream()))
- {
- var result = streamReader.ReadToEnd();
- var jsonResult = JsonConvert.DeserializeObject<TResult>(result);
- return jsonResult;
- }
- }
- public async Task<JObject> CreateAuthenticationPostRequest(string userName, string password)
- {
- string url = ConfigurationManager.AppSettings[AppConfigKeys.OAuthUrl];
- string contentType = @"application/x-www-form-urlencoded";
- var request = WebRequest.Create(url) as HttpWebRequest;
- request.ContentType = contentType;
- request.Method = "POST";
- request.Headers.Add("app_id", "1");
- request.Headers.Add("device_id", "test123");
- using (var writer = new StreamWriter(request.GetRequestStream()))
- {
- string postData = $"username={userName}&password={password}&grant_type=password";
- await writer.WriteAsync(postData);
- }
- try
- {
- var response = await request.GetResponseAsync() as HttpWebResponse;
- using (var reader = new StreamReader(response.GetResponseStream()))
- {
- var result = await reader.ReadToEndAsync();
- return (JObject)JsonConvert.DeserializeObject(result);
- }
- }
- catch (WebException ex)
- {
- MessageBox.Show($"Invalid username or password.{Environment.NewLine} Please try again",
- "Login Error",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error);
- return null;
- }
- }
- public async Task<UserInfo> GetUserInfo(string userName, string accessToken)
- {
- string url = ConfigurationManager.AppSettings[AppConfigKeys.GetUserInfoUrl];
- var userInfo = new UserInfo();
- var request = WebRequest.Create(url) as HttpWebRequest;
- request.Method = "GET";
- request.Headers.Add("Authorization", $"Bearer {accessToken}");
- var response = request.GetResponse() as HttpWebResponse;
- using (var streamReader = new StreamReader(response.GetResponseStream()))
- {
- var result = await streamReader.ReadToEndAsync();
- var parsedResult = JObject.Parse(result);
- userInfo.IsMutisite = parsedResult["isMutisite"].Value<bool>();
- if (userInfo.IsMutisite)
- {
- var jToken = parsedResult["properties"];
- userInfo.Properties = jToken.ToObject<Dictionary<int, string>>();
- }
- else
- {
- userInfo.DefaultPropertyKey = parsedResult["defaultPropertyKey"].Value<int>();
- userInfo.DefaultProperty = parsedResult["defaultProperty"].Value<string>();
- }
- return userInfo;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement