A native .NET C# library for developing Matrix clients.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.8 KiB

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
namespace MatrixDotNetLib
{
public class MatrixSessionManager
{
public MatrixLoginResponse Login(string server, string user, string pass, string device = "")
{
MatrixLoginIdentifier userId = new MatrixLoginIdentifier()
{
IdType = "m.id.user",
User = user
};
MatrixLoginRequest theRequest = new MatrixLoginRequest()
{
Password = pass,
Identifier = userId,
LoginType = "m.login.password"
};
if (device.Length > 0)
{
theRequest.DeviceId = device;
}
// create a web request
string loginUrl = "https://" + server + MatrixApis.User["login"];
// serialize object into JSON
string requestJson = JsonConvert.SerializeObject(theRequest);
HttpContent requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(loginUrl);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage responseMessage = client.PostAsync(loginUrl, requestContent).Result;
string responseString = responseMessage.Content.ReadAsStringAsync().Result;
MatrixLoginResponse response = JsonConvert.DeserializeObject<MatrixLoginResponse>(responseString);
return response;
}
}
}