Авторизация с шифрованием в auth

This commit is contained in:
Mikhalkovich Stanislav 2022-08-14 15:54:41 +03:00
parent e71d14dab2
commit 1392b3b93f
5 changed files with 67 additions and 17 deletions

View file

@ -29,11 +29,18 @@ namespace DBAccessPluginNamespace
var res = true;
try
{
using (var sw = new System.IO.StreamWriter(filename))
var encryptedstring = login + (char)10 + pass;
var encr = LoginUtils.Encrypt(encryptedstring);
using (var fs = new FileStream(filename, FileMode.Create))
{
fs.Write(encr, 0, encr.Length);
}
// записать массив байт в бинарный файл
/*using (var sw = new System.IO.StreamWriter(filename))
{
sw.WriteLine(login);
sw.WriteLine(pass);
}
}*/
}
catch (Exception)
{

View file

@ -16,7 +16,7 @@ namespace VisualPascalABCPlugins
var mbsList = mbs.Get();
foreach (ManagementObject mo in mbsList)
{
return mo['ProcessorId'].ToString();
return mo["ProcessorId"].ToString();
}
return "";
}

View file

@ -43,8 +43,11 @@ namespace DBAccessPluginNamespace
if (UserType != UserTypeEnum.Student) return "Cannot write activity, because you're teacher!";
// Если клиент ещё не создан был, пересоздаём
if (client == null) client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(10);
if (client == null)
{
client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(10);
}
// Словарик для параметров запроса
var values = new Dictionary<string, string>
{
@ -79,8 +82,11 @@ namespace DBAccessPluginNamespace
async public Task<string> GetContents()
{
// Если клиент ещё не создан был, пересоздаём
if (client == null) client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(10);
if (client == null)
{
client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(10);
}
// Тут не столь важно что отправлять, можно и пустой список параметров
var values = new Dictionary<string, string>
{
@ -103,8 +109,11 @@ namespace DBAccessPluginNamespace
if (ServAddr == "") return "Error";
// Если клиент ещё не создан был, пересоздаём
if (client == null) client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(10);
if (client == null)
{
client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(10);
}
var values = new Dictionary<string, string>();
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync(ServAddr + "/groupslist.php", content);
@ -122,8 +131,11 @@ namespace DBAccessPluginNamespace
ServAddr = ServerAddr;
if (ServAddr == "") return "Error";
// Если клиент ещё не создан был, пересоздаём
if (client == null) client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(10);
if (client == null)
{
client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(10);
}
Group = GroupName;
var values = new Dictionary<string, string>
{
@ -148,8 +160,11 @@ namespace DBAccessPluginNamespace
ShortFIO = shortFIO;
FullFIO = fullFIO;
Password = password;
if (client == null) client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(10);
if (client == null)
{
client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(10);
}
// Словарик для параметров запроса
var values = new Dictionary<string, string>

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Linq;
using PascalABCCompiler.SyntaxTreeConverters;
using PascalABCCompiler.SyntaxTree;
@ -165,6 +166,22 @@ namespace VisualPascalABCPlugins
}
}
string[] ReadLoginPassFromAuth(string AuthFileFullName)
{
var n = 10000;
var arr = new byte[n];
int nbytes;
using (var fs = new FileStream(AuthFileFullName, FileMode.Open))
{
nbytes = fs.Read(arr, 0, n);
}
var data = new byte[nbytes];
System.Array.Copy(arr, data, nbytes);
var lp = LoginUtils.Decrypt(data);
var lparr = lp.Split((char)10);
return lparr;
}
public void AfterAddInGUI()
{
menuItem = (ToolStripMenuItem)Item.menuItem;
@ -191,12 +208,18 @@ namespace VisualPascalABCPlugins
// искать auth в папке на уровень выше
auth = System.IO.Path.Combine(WorkingDir, "..", "auth.dat");
if (System.IO.File.Exists(auth))
AuthFileFullName = auth;
{
var fi = new System.IO.FileInfo(auth);
AuthFileFullName = fi.FullName;
}
}
}
// Попытка автоматического входа
if (AuthFileFullName != "")
{
var ss = System.IO.File.ReadAllLines(AuthFileFullName);
//var ss = System.IO.File.ReadAllLines(AuthFileFullName);
var ss = ReadLoginPassFromAuth(AuthFileFullName);
if (ss.Length >= 2)
{
var login = ss[0];
@ -205,8 +228,10 @@ namespace VisualPascalABCPlugins
}
}
}
catch (Exception)
{ }
catch (Exception e)
{
e = e;
}
}
private void RunStartingHandler(string filename)

View file

@ -59,7 +59,9 @@
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Security" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
@ -70,6 +72,7 @@
<Compile Include="LoginForm.designer.cs">
<DependentUpon>LoginForm.cs</DependentUpon>
</Compile>
<Compile Include="LoginUtils.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>