ASP.NET Razor Pages:

now describe below,
Create a project from scratch.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
namespace Authentication.Pages
{
public class LoginModel : PageModel
{
public void OnGet()
{
}
[BindProperty]
[Required]
[Display(Name = "Email")]
public string Email { get; set; }
[BindProperty]
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
public async Task<JsonResult> OnPost(string Email, string Password)
{
try
{
var isValidUser = Email == "shadab33@hotmail.com" && Password == "111111";
if (isValidUser == null)
{
ModelState.AddModelError("", "Invalid user");
}
if (!ModelState.IsValid)
{
return new JsonResult("false");
}
var scheme = CookieAuthenticationDefaults.AuthenticationScheme;
var AuthUser = new ClaimsPrincipal(
new ClaimsIdentity(
new[] { new Claim(ClaimTypes.Name, Email) },
scheme
)
);
await HttpContext.SignInAsync(scheme, AuthUser);
return new JsonResult("true");
}
catch (DbUpdateConcurrencyException ex)
{
throw ex;
}
}
}
}
and Html code here
@model Authentication.Pages.LoginModel
@{
ViewData["Title"] = "Login";
}
<h2>Login</h2>
<form method="post">
<input type="text" placeholder="Email" name="Email" id="Email" autocomplete="off">
<input type="password" placeholder="Password" id="Password" name="Password">
<button type="submit">Sign In </button>
</form>
now authentication is complete code is done
if you can learn about Asp.net MVC.
and if you can learn about Asp.net Razor pages
0 Comments