C Sharp MVC Razor code for a login screen and its controller
By: Niraj in csharp Tutorials on 2023-03-22
Here are the steps to create a login screen in a CSHTML view file and the associated controller code in C#:
Step 1: Create a login form in a CSHTML view file In your CSHTML view file (e.g. Login.cshtml), you can create a login form using HTML and Razor syntax:
@model LoginViewModel
<form method="post" asp-action="Login">
<label for="username">Username:</label>
<input type="text" id="username" name="username" />
<br />
<label for="password">Password:</label>
<input type="password" id="password" name="password" />
<br />
<input type="submit" value="Log in" />
</form>
This form has two fields for the user to enter their username and password, and a submit button to submit the form.
Step 2: Create a LoginViewModel to represent the form fields Create a LoginViewModel class to represent the form fields:
public class LoginViewModel { public string Username { get; set; } public string Password { get; set; } }
This class will be used to bind the form fields to the controller action.
Step 3: Create a Login action in the controller In your controller (e.g. AccountController.cs), create a Login action to handle the form submission:
[HttpGet] public IActionResult Login() { return View(); } [HttpPost] public IActionResult Login(LoginViewModel model) { // TODO: Check the user's credentials and authenticate the user if (model.Username == "admin" && model.Password == "password") { return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", "Invalid login attempt"); return View(model); } }
The first Login action handles the GET request and returns the Login view. The second Login action handles the POST request and checks the user's credentials. If the user's credentials are valid, the action redirects to the Home controller's Index action. If the credentials are invalid, it adds a model error and returns the Login view with the invalid model.
Step 4: Render the validation errors in the view In the Login.cshtml view, add the following code to render the validation errors if the user's login attempt is invalid:
@model LoginViewModel
<form method="post" asp-action="Login">
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="@Model.Username" />
<br />
<label for="password">Password:</label>
<input type="password" id="password" name="password" />
<br />
<input type="submit" value="Log in" />
<br />
<span asp-validation-summary="All" class="text-danger"></span>
<span asp-validation-for="Username" class="text-danger"></span>
<span asp-validation-for="Password" class="text-danger"></span>
</form>
This code will render any validation errors in the view, including a summary of all errors and any errors associated with the username and password fields.
That's it! You now have a basic login screen and controller in C#. Of course, you will need to modify this code to fit your specific needs and integrate it into your application accordingly.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in csharp ) |
Latest Articles (in csharp) |
Comments