0% found this document useful (0 votes)
13 views

Logi&registration

Uploaded by

gadala.suhasini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Logi&registration

Uploaded by

gadala.suhasini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

using Microsoft.AspNetCore.

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.");
}

var user = new User


{
Username = model.Username,
Password = model.Password // In real life, hash the password before
saving
};

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.");
}

// In real life, issue a JWT token here instead of just returning a


message
return Ok("Login successful.");
}
}

public class RegisterModel


{
[Required]
public string Username { get; set; }

[Required]
public string Password { get; set; }

[Compare("Password", ErrorMessage = "The password and confirmation password


do not match.")]
public string ConfirmPassword { get; set; }
}

public class LoginModel


{
[Required]
public string Username { get; set; }

[Required]
public string Password { get; set; }
}

public class User


{
public string Username { get; set; }
public string Password { get; set; }
}
}
-------------------------------------------------
@page
@model YourApp.Pages.Account.RegisterModel

@{
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>

<div id="errorMessage" class="alert alert-danger" style="display:none;"></div>

@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; }

public void OnGet()


{
}

public IActionResult OnPost()


{
return Page(); // This can be handled with JavaScript AJAX (See
Register.cshtml)
}
}
}
-----------------------------------------------
@page
@model YourApp.Pages.Account.LoginModel

@{
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>

<div id="errorMessage" class="alert alert-danger" style="display:none;"></div>

@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; }

public void OnGet()


{
}

public IActionResult OnPost()


{
return Page(); // This can be handled with JavaScript AJAX (See
Login.cshtml)
}
}
}

You might also like