Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1) ---- Model -----
- public class User
- {
- [Key]
- public int UserId { get; set; }
- [Column(TypeName = "nvarchar(100)")]
- public string Username { get; set; }
- [PassValidation(10)]
- [Column(TypeName = "nvarchar(100)")]
- public string Password { get; set; }
- [Display(Name = "Confirm password")]
- [NotMapped]
- public string ConfirmPassword { get; set; }
- [Column(TypeName = "int")]
- public int Age { get; set; }
- }
- 2) -- DB context --
- public class ApplicationDbContext : DbContext
- {
- public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
- : base(options)
- {
- }
- public DbSet<User> users { get; set; }
- }
- 3) -- appsettings.json -> Add connection string
- "ConnectionStrings": {
- "UserDbStr": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=UsersDB;"
- }
- 4) --.... Startup.cs -> void Configure()...
- services.AddDbContext<ApplicationDbContext>(dbContOp => dbContOp.UseSqlServer(Configuration.GetConnectionString("UserDbStr")));
- 5) -- Package manager Console ---
- Add-Migration "InitialCreate"
- Update-Database
- 6) -- Scaffold
- 7) -- Custom validation
- public class PassValidation : ValidationAttribute
- {
- private readonly int _minL;
- public PassValidation(int minL)
- {
- this._minL = minL;
- }
- public override bool IsValid(object value)
- {
- if (value == null)
- {
- return false;
- }
- string pass = value as string;
- if (pass.Length < _minL)
- {
- return false;
- }
- return true;
- }
- }
- 8) -- Add Session
- 8.1 ... public void ConfigureServices(IServiceCollection services)....
- services.AddSession(options => {
- options.IdleTimeout = TimeSpan.FromMinutes(60); //You can set Time
- });
- 8.2 ... public void Configure()...
- app.UseSession();
- 9. -- Hash SHA256 --
- public class Crypto
- {
- public static string ComputeSha256Hash(string rawData)
- {
- // Create a SHA256
- using (SHA256 sha256Hash = SHA256.Create())
- {
- // ComputeHash - returns byte array
- byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
- // Convert byte array to a string
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < bytes.Length; i++)
- {
- builder.Append(bytes[i].ToString("x2"));
- }
- return builder.ToString();
- }
- }
- }
- 10 --- Login Action ---
- public async Task<IActionResult> Login(string Uname, string Pass)
- {
- string hPass = Utilities.Crypto.ComputeSha256Hash(Pass);
- var user = await _context.users
- .FirstOrDefaultAsync(m => m.Username == Uname && m.Password == hPass);
- if (user == null)
- {
- ViewData["result"] = "Invalid username or password!";
- return View();
- }
- HttpContext.Session.Set("Username", Encoding.UTF8.GetBytes(user.Username));
- HttpContext.Session.Set("Age", Encoding.UTF8.GetBytes(user.Age.ToString()));
- return RedirectToAction("Index", "WebSystem");
- }
- 11. Log to system
- public IActionResult Index()
- {
- byte[] buffer = new byte[100];
- bool hasuser = HttpContext.Session.TryGetValue("Username", out buffer);
- if (hasuser)
- {
- ViewData["user"] = Encoding.UTF8.GetString(buffer);
- }
- bool hasage = HttpContext.Session.TryGetValue("Age", out buffer);
- if(hasage)
- {
- ViewData["age"] = int.Parse(Encoding.UTF8.GetString(buffer));
- }
- else
- {
- ViewData["age"] = "---";
- }
- if (hasuser)
- {
- return View();
- }
- else
- {
- return RedirectToAction("Login", "Users");
- }
- }
- 12. --- Logout --
- public IActionResult Logout()
- {
- HttpContext.Session.Clear();
- return RedirectToAction("Login", "Users");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement