Logi®istration
Logi®istration
Mvc;
using System.ComponentModel.DataAnnotations;
namespace YourApp.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AccountController : ControllerBase
{
// This is a simple model to simulate the registration and login
functionality
// You can replace it with real authentication logic (e.g., using Identity,
JWTs)
private static List<User> users = new List<User>();
[HttpPost("register")]
public IActionResult Register([FromBody] RegisterModel model)
{
if (users.Any(u => u.Username == model.Username))
{
return BadRequest("User already exists.");
}
users.Add(user);
return Ok("User registered successfully.");
}
[HttpPost("login")]
public IActionResult Login([FromBody] LoginModel model)
{
var user = users.SingleOrDefault(u => u.Username == model.Username &&
u.Password == model.Password);
if (user == null)
{
return Unauthorized("Invalid credentials.");
}
[Required]
public string Password { get; set; }
[Required]
public string Password { get; set; }
}
@{
ViewData["Title"] = "Register";
}
<h1>@ViewData["Title"]</h1>
<form method="post">
<div class="form-group">
<label for="Username">Username</label>
<input type="text" class="form-control" id="Username" name="Username"
required />
</div>
<div class="form-group">
<label for="Password">Password</label>
<input type="password" class="form-control" id="Password" name="Password"
required />
</div>
<div class="form-group">
<label for="ConfirmPassword">Confirm Password</label>
<input type="password" class="form-control" id="ConfirmPassword"
name="ConfirmPassword" required />
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
@section Scripts {
<script>
$("form").submit(function (e) {
e.preventDefault();
const data = {
Username: $("#Username").val(),
Password: $("#Password").val(),
ConfirmPassword: $("#ConfirmPassword").val()
};
$.ajax({
url: "/api/account/register",
type: "POST",
contentType: "application/json",
data: JSON.stringify(data),
success: function (response) {
alert(response);
window.location.href = "/Login"; // Redirect to login page
},
error: function (response) {
$("#errorMessage").text(response.responseText).show();
}
});
});
</script>
}
----------------------------------------------
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace YourApp.Pages.Account
{
public class RegisterModel : PageModel
{
[BindProperty]
public string Username { get; set; }
[BindProperty]
public string Password { get; set; }
[BindProperty]
public string ConfirmPassword { get; set; }
@{
ViewData["Title"] = "Login";
}
<h1>@ViewData["Title"]</h1>
<form method="post">
<div class="form-group">
<label for="Username">Username</label>
<input type="text" class="form-control" id="Username" name="Username"
required />
</div>
<div class="form-group">
<label for="Password">Password</label>
<input type="password" class="form-control" id="Password" name="Password"
required />
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
@section Scripts {
<script>
$("form").submit(function (e) {
e.preventDefault();
const data = {
Username: $("#Username").val(),
Password: $("#Password").val()
};
$.ajax({
url: "/api/account/login",
type: "POST",
contentType: "application/json",
data: JSON.stringify(data),
success: function (response) {
alert(response);
window.location.href = "/"; // Redirect to homepage or another
page after login
},
error: function (response) {
$("#errorMessage").text(response.responseText).show();
}
});
});
</script>
}
------------------------------------------------
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace YourApp.Pages.Account
{
public class LoginModel : PageModel
{
[BindProperty]
public string Username { get; set; }
[BindProperty]
public string Password { get; set; }