добавляет базовую авторизацию через SQLite
This commit is contained in:
parent
3d8060d3fd
commit
dfff6faacc
87
Authorization.cs
Normal file
87
Authorization.cs
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using System.IO;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
|
||||||
|
namespace AwesomeEmailExtractor
|
||||||
|
{
|
||||||
|
public class Authorization
|
||||||
|
{
|
||||||
|
public static User Login(string login, string password)
|
||||||
|
{
|
||||||
|
SqliteCommand command = new SqliteCommand();
|
||||||
|
command.Connection = Globals.db;
|
||||||
|
command.CommandText = "SELECT login, role_id FROM users WHERE login = @login AND password = @password";
|
||||||
|
|
||||||
|
SqliteParameter loginParam = new SqliteParameter("@login", login);
|
||||||
|
command.Parameters.Add(loginParam);
|
||||||
|
|
||||||
|
SqliteParameter passwordParam = new SqliteParameter("@password", EncryptPassword(password));
|
||||||
|
command.Parameters.Add(passwordParam);
|
||||||
|
|
||||||
|
SqliteDataReader reader = command.ExecuteReader();
|
||||||
|
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
return new User(reader.GetString(0), (UserRoles)reader.GetInt32(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Exception("Пользователь не найден!");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static User Register(string login, string password)
|
||||||
|
{
|
||||||
|
SqliteCommand command = new SqliteCommand();
|
||||||
|
command.Connection = Globals.db;
|
||||||
|
command.CommandText = "INSERT INTO users (login, password, role_id) VALUES (@login, @password, 0);";
|
||||||
|
|
||||||
|
SqliteParameter loginParam = new SqliteParameter("@login", login);
|
||||||
|
command.Parameters.Add(loginParam);
|
||||||
|
|
||||||
|
SqliteParameter passwordParam = new SqliteParameter("@password", EncryptPassword(password));
|
||||||
|
command.Parameters.Add(passwordParam);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
} catch (SqliteException e)
|
||||||
|
{
|
||||||
|
if (e.SqliteErrorCode == 19) {
|
||||||
|
throw new Exception("Имя пользователя занятно!");
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Exception($"Ошибка: {e.Message}");
|
||||||
|
};
|
||||||
|
|
||||||
|
return new User(login, UserRoles.DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string EncryptPassword(string password)
|
||||||
|
{
|
||||||
|
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
|
||||||
|
{
|
||||||
|
UTF8Encoding utf8 = new UTF8Encoding();
|
||||||
|
byte[] data = md5.ComputeHash(utf8.GetBytes(password));
|
||||||
|
return Convert.ToBase64String(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum UserRoles
|
||||||
|
{
|
||||||
|
DEFAULT,
|
||||||
|
ADMIN
|
||||||
|
}
|
||||||
|
public class User
|
||||||
|
{
|
||||||
|
public string Login { get; set; }
|
||||||
|
public UserRoles Role { get; }
|
||||||
|
|
||||||
|
public User(string login, UserRoles role)
|
||||||
|
{
|
||||||
|
Login = login;
|
||||||
|
Role = role;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,8 @@
|
|||||||
<FileAlignment>512</FileAlignment>
|
<FileAlignment>512</FileAlignment>
|
||||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||||
<Deterministic>true</Deterministic>
|
<Deterministic>true</Deterministic>
|
||||||
|
<NuGetPackageImportStamp>
|
||||||
|
</NuGetPackageImportStamp>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
@ -33,8 +35,36 @@
|
|||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="Microsoft.Data.Sqlite, Version=5.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\Microsoft.Data.Sqlite.Core.5.0.3\lib\netstandard2.0\Microsoft.Data.Sqlite.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="SQLitePCLRaw.batteries_v2, Version=2.0.4.976, Culture=neutral, PublicKeyToken=8226ea5df37bcae9, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\SQLitePCLRaw.bundle_e_sqlite3.2.0.4\lib\net461\SQLitePCLRaw.batteries_v2.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="SQLitePCLRaw.core, Version=2.0.4.976, Culture=neutral, PublicKeyToken=1488e028ca7ab535, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\SQLitePCLRaw.core.2.0.4\lib\netstandard2.0\SQLitePCLRaw.core.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="SQLitePCLRaw.nativelibrary, Version=2.0.4.976, Culture=neutral, PublicKeyToken=502ed628492ab262, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\SQLitePCLRaw.bundle_e_sqlite3.2.0.4\lib\net461\SQLitePCLRaw.nativelibrary.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="SQLitePCLRaw.provider.dynamic_cdecl, Version=2.0.4.976, Culture=neutral, PublicKeyToken=b68184102cba0b3b, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\SQLitePCLRaw.provider.dynamic_cdecl.2.0.4\lib\netstandard2.0\SQLitePCLRaw.provider.dynamic_cdecl.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Numerics" />
|
||||||
|
<Reference Include="System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
<Reference Include="System.Data.DataSetExtensions" />
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
@ -46,7 +76,9 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="Authorization.cs" />
|
||||||
<Compile Include="ExtactEmailsAlgorithm.cs" />
|
<Compile Include="ExtactEmailsAlgorithm.cs" />
|
||||||
|
<Compile Include="Globals.cs" />
|
||||||
<Compile Include="MainForm.cs">
|
<Compile Include="MainForm.cs">
|
||||||
<SubType>Form</SubType>
|
<SubType>Form</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
@ -67,6 +99,7 @@
|
|||||||
<AutoGen>True</AutoGen>
|
<AutoGen>True</AutoGen>
|
||||||
<DependentUpon>Resources.resx</DependentUpon>
|
<DependentUpon>Resources.resx</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<None Include="packages.config" />
|
||||||
<None Include="Properties\Settings.settings">
|
<None Include="Properties\Settings.settings">
|
||||||
<Generator>SettingsSingleFileGenerator</Generator>
|
<Generator>SettingsSingleFileGenerator</Generator>
|
||||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
||||||
@ -81,4 +114,11 @@
|
|||||||
<None Include="App.config" />
|
<None Include="App.config" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
<Import Project="packages\SQLitePCLRaw.lib.e_sqlite3.2.0.4\build\net461\SQLitePCLRaw.lib.e_sqlite3.targets" Condition="Exists('packages\SQLitePCLRaw.lib.e_sqlite3.2.0.4\build\net461\SQLitePCLRaw.lib.e_sqlite3.targets')" />
|
||||||
|
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||||
|
<PropertyGroup>
|
||||||
|
<ErrorText>Данный проект ссылается на пакеты NuGet, отсутствующие на этом компьютере. Используйте восстановление пакетов NuGet, чтобы скачать их. Дополнительную информацию см. по адресу: http://go.microsoft.com/fwlink/?LinkID=322105. Отсутствует следующий файл: {0}.</ErrorText>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Error Condition="!Exists('packages\SQLitePCLRaw.lib.e_sqlite3.2.0.4\build\net461\SQLitePCLRaw.lib.e_sqlite3.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\SQLitePCLRaw.lib.e_sqlite3.2.0.4\build\net461\SQLitePCLRaw.lib.e_sqlite3.targets'))" />
|
||||||
|
</Target>
|
||||||
</Project>
|
</Project>
|
68
Globals.cs
Normal file
68
Globals.cs
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.IO;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
|
||||||
|
|
||||||
|
namespace AwesomeEmailExtractor
|
||||||
|
{
|
||||||
|
internal class Globals
|
||||||
|
{
|
||||||
|
// Getter and setter for SQLite database connection
|
||||||
|
public static SqliteConnection db { get; set; }
|
||||||
|
|
||||||
|
public static string getAppDirectory()
|
||||||
|
{
|
||||||
|
return Path.Combine(
|
||||||
|
Environment.GetFolderPath(
|
||||||
|
Environment.SpecialFolder.ApplicationData
|
||||||
|
),
|
||||||
|
"AwesomeEmailExtractor"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string getAppDatabase()
|
||||||
|
{
|
||||||
|
return Path.Combine(
|
||||||
|
getAppDirectory(),
|
||||||
|
"database.db"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void CreateTables()
|
||||||
|
{
|
||||||
|
SqliteCommand command = new SqliteCommand();
|
||||||
|
command.Connection = db;
|
||||||
|
|
||||||
|
// Создать таблицу для хранения ролей
|
||||||
|
command.CommandText = "CREATE TABLE IF NOT EXISTS roles (id INTEGER PRIMARY KEY, name TEXT NOT NULL);";
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
|
||||||
|
// Добавить роли
|
||||||
|
command.CommandText = "INSERT OR IGNORE INTO roles (id, name) VALUES (0, 'DEFAULT'), (1, 'ADMIN');";
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
|
||||||
|
// Создать таблицу для хранения пользователей
|
||||||
|
command.CommandText = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, login TEXT NOT NULL UNIQUE, password TEXT NOT NULL, role_id INTEGER NOT NULL, FOREIGN KEY(role_id) REFERENCES roles(id));";
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
|
||||||
|
// Если таблица пуста - добавить пользователя по умолчанию
|
||||||
|
command.CommandText = "SELECT COUNT(*) FROM users";
|
||||||
|
|
||||||
|
if (Convert.ToInt32(command.ExecuteScalar()) == 0)
|
||||||
|
{
|
||||||
|
command.CommandText = "INSERT INTO users (login, password, role_id) VALUES ('admin', @password, 1);";
|
||||||
|
|
||||||
|
SqliteParameter passwordParam = new SqliteParameter("@password", Authorization.EncryptPassword("admin"));
|
||||||
|
command.Parameters.Add(passwordParam);
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
command.Parameters.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
Program.cs
24
Program.cs
@ -3,6 +3,8 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
using System.IO;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
|
||||||
namespace AwesomeEmailExtractor
|
namespace AwesomeEmailExtractor
|
||||||
{
|
{
|
||||||
@ -14,9 +16,31 @@ namespace AwesomeEmailExtractor
|
|||||||
[STAThread]
|
[STAThread]
|
||||||
static void Main()
|
static void Main()
|
||||||
{
|
{
|
||||||
|
preMain();
|
||||||
|
|
||||||
Application.EnableVisualStyles();
|
Application.EnableVisualStyles();
|
||||||
Application.SetCompatibleTextRenderingDefault(false);
|
Application.SetCompatibleTextRenderingDefault(false);
|
||||||
Application.Run(new MainForm());
|
Application.Run(new MainForm());
|
||||||
|
|
||||||
|
postMain();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void preMain()
|
||||||
|
{
|
||||||
|
if (!Directory.Exists(Globals.getAppDirectory()))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(Globals.getAppDirectory());
|
||||||
|
}
|
||||||
|
|
||||||
|
Globals.db = new SqliteConnection("Data Source=" + Globals.getAppDatabase());
|
||||||
|
Globals.db.Open();
|
||||||
|
|
||||||
|
Globals.CreateTables();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void postMain()
|
||||||
|
{
|
||||||
|
Globals.db.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
packages.config
Normal file
13
packages.config
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<packages>
|
||||||
|
<package id="Microsoft.Data.Sqlite" version="5.0.3" targetFramework="net472" />
|
||||||
|
<package id="Microsoft.Data.Sqlite.Core" version="5.0.3" targetFramework="net472" />
|
||||||
|
<package id="SQLitePCLRaw.bundle_e_sqlite3" version="2.0.4" targetFramework="net472" />
|
||||||
|
<package id="SQLitePCLRaw.core" version="2.0.4" targetFramework="net472" />
|
||||||
|
<package id="SQLitePCLRaw.lib.e_sqlite3" version="2.0.4" targetFramework="net472" />
|
||||||
|
<package id="SQLitePCLRaw.provider.dynamic_cdecl" version="2.0.4" targetFramework="net472" />
|
||||||
|
<package id="System.Buffers" version="4.4.0" targetFramework="net472" />
|
||||||
|
<package id="System.Memory" version="4.5.3" targetFramework="net472" />
|
||||||
|
<package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net472" />
|
||||||
|
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net472" />
|
||||||
|
</packages>
|
Reference in New Issue
Block a user