Commit bd519fc6 authored by Kirill's avatar Kirill

Авторизация и сохранение/восстановление куков

parent 84d38e51
using System;
using System.Collections.Generic;
using System.Net.Http;
using Ninject;
namespace TourDataManager {
public class Authenticator : IAuthenticator {
private readonly Uri authUri = new Uri("https://biganto.com/users/login/?client=desktopplayer&client_version=3.0&v=2.0");
[Inject] public HttpClient HttpClient{ get; set; }
public async void Login(string email, string password, Action<bool, string> continuation = null){
var x = await HttpClient.PostAsync(authUri,
new FormUrlEncodedContent(new[]{
new KeyValuePair<string, string>("email", email),
new KeyValuePair<string, string>("password", password)
}));
var content = await x.Content.ReadAsStringAsync();
continuation?.Invoke(x.IsSuccessStatusCode,content);
}
public void Logout(){
}
}
}
\ No newline at end of file
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
namespace TourDataManager {
public class CookieStorage {
private readonly string cookiePath;
private CookieContainer CookieContainer;
public CookieStorage(string cookiePath){
this.cookiePath = cookiePath;
}
public CookieContainer Get(){
if (CookieContainer != null) return CookieContainer;
CookieContainer = ReadCookiesFromDisk(cookiePath);
Debug.Log($"Count of cookies restored : {CookieContainer.Count}");
return CookieContainer;
}
public void Save(CookieContainer cookieContainer){
WriteCookiesToDisk(cookiePath, cookieContainer);
Debug.Log($"Count of cookies saved : {cookieContainer}");
}
public void Save(){
Save(CookieContainer);
}
public static void WriteCookiesToDisk(string file, CookieContainer cookieJar){
using(Stream stream = File.Create(file)){
try {
Debug.Log("Writing cookies to disk... ");
var formatter = new BinaryFormatter();
formatter.Serialize(stream, cookieJar);
Debug.Log("Done.");
} catch(Exception e) {
Debug.LogError("Problem writing cookies to disk: " + e.GetType());
}
}
}
public static CookieContainer ReadCookiesFromDisk(string file){
try {
using(Stream stream = File.Open(file, FileMode.Open)){
Debug.Log("Reading cookies from disk... ");
var formatter = new BinaryFormatter();
Debug.Log("Done.");
return (CookieContainer)formatter.Deserialize(stream);
}
} catch(Exception e) {
Debug.LogError("Problem reading cookies from disk: " + e.GetType());
return new CookieContainer();
}
}
}
}
\ No newline at end of file
using System;
namespace TourDataManager {
public static class Debug {
public static void Log(object msg){
Console.WriteLine(msg);
}
public static void LogError(object msg){
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(msg);
Console.ResetColor();
}
}
}
\ No newline at end of file
using System;
namespace TourDataManager {
public interface IAuthenticator {
void Login(string email, string password, Action<bool,string> continuation = null);
void Logout();
}
}
\ No newline at end of file
namespace TourDataManager {
public class MyUtilities {
public int c;
public void AddValues(int a, int b) {
c = a + b;
var t1 = (1, 2);
var t2 = (1, 2);
if (t1 == t2){
}
}
/// C# 7.3 feature
public static bool CompareTuples((int,int) a,(int,int) b){ return a == b; }
public static int GenerateRandom(int min, int max) {
System.Random rand = new System.Random();
return rand.Next(min, max);
}
}
}
\ No newline at end of file
using System.Net.Http;
using Ninject;
using Ninject.Modules;
namespace TourDataManager {
public class TourDataManager {
private string persistentPath;
private IKernel Container;
public TourDataManager(string persistentPath){
this.persistentPath = persistentPath;
Container = new StandardKernel(new MyModule(persistentPath));
}
public void Login(string email, string password){
Container.Get<IAuthenticator>().Login(email,password, (b, s) => {
var cookiestor = Container.Get<CookieStorage>();
Debug.Log($"Authorization : {s}");
Debug.Log($"Cookie count in storage : {cookiestor.Get().Count}");
cookiestor.Save();
});
}
}
public class MyModule : NinjectModule {
private readonly string persistentPath;
public MyModule(string persistentPath){
this.persistentPath = persistentPath;
}
public override void Load(){
var cookieStorage = new CookieStorage(persistentPath + "\\cookie.storage");
var httpClientHandler = new HttpClientHandler();
//CookieContainer сам запоминает полученные из ответа куки
httpClientHandler.CookieContainer = cookieStorage.Get();
var httpClient = new HttpClient(httpClientHandler);
Bind<CookieStorage>().ToConstant(cookieStorage).InSingletonScope();
Bind<HttpClientHandler>().ToConstant(httpClientHandler).InSingletonScope();
Bind<HttpClient>().ToConstant(httpClient).InSingletonScope();
Bind<IAuthenticator>().To<Authenticator>().InSingletonScope();
}
}
}
\ No newline at end of file
......@@ -33,14 +33,74 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="Ninject, Version=3.3.4.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7">
<HintPath>..\packages\Ninject.3.3.4\lib\net45\Ninject.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.IO, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\System.IO.4.3.0\lib\net462\System.IO.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\System.Net.Http.4.3.3\lib\net46\System.Net.Http.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Reactive, Version=4.1.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263">
<HintPath>..\packages\System.Reactive.4.1.1\lib\net46\System.Reactive.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net463\System.Security.Cryptography.Algorithms.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Encoding, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.X509Certificates, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
<HintPath>..\packages\System.ValueTuple.4.4.0\lib\net47\System.ValueTuple.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Windows" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="MyUtilities.cs" />
<Compile Include="Authenticator.cs" />
<Compile Include="CookieStorage.cs" />
<Compile Include="Debug.cs" />
<Compile Include="IAuthenticator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TourDataManager.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Ninject" version="3.3.4" targetFramework="net472" />
<package id="System.IO" version="4.3.0" targetFramework="net472" />
<package id="System.Net.Http" version="4.3.3" targetFramework="net472" />
<package id="System.Reactive" version="4.1.1" targetFramework="net472" />
<package id="System.Runtime" version="4.3.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.0" targetFramework="net472" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net472" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net472" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net472" />
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.1" targetFramework="net472" />
<package id="System.ValueTuple" version="4.4.0" targetFramework="net472" />
</packages>
\ No newline at end of file
......@@ -3,9 +3,26 @@ using TourDataManager;
namespace TourDataManagerConsoleApplication {
internal class Program {
const string PersistentPath = "C:\\Users\\Bigantounity\\AppData\\LocalLow\\Biganto\\StandalonePlayer";
const string defaultLogin = "demo@biganto.ru";
const string defaultPassword = "demo";
public static void Main(string[] args){
var isEqual = MyUtilities.CompareTuples((1, 2), (1, 2));
Console.Write(isEqual);
var tourDataManager = new TourDataManager.TourDataManager(PersistentPath);
tourDataManager.Login(defaultLogin, defaultPassword);
Console.Read();
}
public static async void Login(){
}
}
}
\ No newline at end of file
......@@ -33,10 +33,34 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="Ninject, Version=3.3.4.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7">
<HintPath>..\packages\Ninject.3.3.4\lib\net45\Ninject.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Reactive, Version=4.1.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263">
<HintPath>..\packages\System.Reactive.4.1.1\lib\net46\System.Reactive.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
<HintPath>..\packages\System.ValueTuple.4.4.0\lib\net47\System.ValueTuple.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Windows" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
......@@ -48,6 +72,9 @@
<Name>TourDataManager</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Ninject" version="3.3.4" targetFramework="net472" />
<package id="System.Reactive" version="4.1.1" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.0" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.1" targetFramework="net472" />
<package id="System.ValueTuple" version="4.4.0" targetFramework="net472" />
</packages>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment