-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Threading.Tasks; | ||
|
||
public class AccountController : Controller | ||
{ | ||
private readonly UserManager<ApplicationUser> _userManager; | ||
private readonly SignInManager<ApplicationUser> _signInManager; | ||
|
||
public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager) | ||
{ | ||
_userManager = userManager; | ||
_signInManager = signInManager; | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult Register() => View(); | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Register(RegisterViewModel model) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; | ||
var result = await _userManager.CreateAsync(user, model.Password); | ||
if (result.Succeeded) | ||
{ | ||
await _signInManager.SignInAsync(user, isPersistent: false); | ||
return RedirectToAction("Index", "Home"); | ||
} | ||
foreach (var error in result.Errors) | ||
ModelState.AddModelError(string.Empty, error.Description); | ||
} | ||
return View(model); | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult Login() => View(); | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Login(LoginViewModel model) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false); | ||
if (result.Succeeded) | ||
return RedirectToAction("Index", "Home"); | ||
|
||
ModelState.AddModelError(string.Empty, "Invalid login attempt."); | ||
} | ||
return View(model); | ||
} | ||
|
||
public async Task<IActionResult> Logout() | ||
{ | ||
await _signInManager.SignOutAsync(); | ||
return RedirectToAction("Index", "Home"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> | ||
{ | ||
public DbSet<Patient> Patients { get; set; } | ||
public DbSet<Appointment> Appointments { get; set; } | ||
|
||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
base.OnModelCreating(modelBuilder); | ||
|
||
modelBuilder.Entity<Patient>() | ||
.Property(p => p.MedicalRecords) | ||
.HasConversion( | ||
v => EncryptData(v), | ||
v => DecryptData(v) | ||
); | ||
} | ||
|
||
private string EncryptData(string data) | ||
{ | ||
if (string.IsNullOrEmpty(data)) return data; | ||
using var aes = Aes.Create(); | ||
aes.Key = Encoding.UTF8.GetBytes("YourSecureKeyHere!"); // Ensure this key is secure | ||
aes.IV = new byte[16]; // Initialization vector | ||
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV); | ||
using var ms = new MemoryStream(); | ||
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) | ||
using (var sw = new StreamWriter(cs)) sw.Write(data); | ||
return Convert.ToBase64String(ms.ToArray()); | ||
} | ||
|
||
private string DecryptData(string encryptedData) | ||
{ | ||
if (string.IsNullOrEmpty(encryptedData)) return encryptedData; | ||
using var aes = Aes.Create(); | ||
aes.Key = Encoding.UTF8.GetBytes("YourSecureKeyHere!"); | ||
aes.IV = new byte[16]; | ||
var decryptor = aes.CreateDecryptor(aes.Key, aes.IV); | ||
using var ms = new MemoryStream(Convert.FromBase64String(encryptedData)); | ||
using var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read); | ||
using var sr = new StreamReader(cs); | ||
return sr.ReadToEnd(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
|
||
public class Appointment | ||
{ | ||
public int Id { get; set; } | ||
public DateTime AppointmentDate { get; set; } | ||
|
||
[Required] | ||
public string CaregiverId { get; set; } | ||
|
||
[Required] | ||
public int PatientId { get; set; } | ||
|
||
public string Notes { get; set; } | ||
public bool IsConfirmed { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class Patient | ||
{ | ||
public int Id { get; set; } | ||
|
||
[Required] | ||
[StringLength(100)] | ||
public string Name { get; set; } | ||
|
||
[Required] | ||
public string Address { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.MultilineText)] | ||
public string MedicalRecords { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Threading.Tasks; | ||
|
||
[Authorize] | ||
public class PatientController : Controller | ||
{ | ||
private readonly ApplicationDbContext _context; | ||
|
||
public PatientController(ApplicationDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult Create() => View(); | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Create(Patient patient) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
_context.Patients.Add(patient); | ||
await _context.SaveChangesAsync(); | ||
return RedirectToAction("Index", "Home"); | ||
} | ||
return View(patient); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dotnet new mvc -n HealthcareSystem | ||
cd HealthcareSystem | ||
dotnet add package Microsoft.EntityFrameworkCore.SqlServer | ||
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore |