ASP NetMVC Core Secure App Use Identity Login - SkillBakery Studios

Breaking

Post Top Ad

Post Top Ad

Saturday, July 11, 2026

ASP NetMVC Core Secure App Use Identity Login

ASP NetMVC Core Secure App Use Identity Login

Screenshot from the tutorial
Screenshot from the tutorial

Building a Secure ASP.NET Core MVC Application with Identity Login

In this blog post, we'll explore how to create a secure ASP.NET Core MVC application that utilizes Identity for user authentication. This guide is targeted towards developers who want to implement user login functionality within a short timeframe, as demonstrated in the YouTube video titled "ASP NetMVC Core Secure App Use Identity Login".

What is ASP.NET Core Identity?

ASP.NET Core Identity is a membership system that adds login functionality to your application. It provides a robust set of features, including:

  • User registration and login
  • Password recovery
  • User roles
  • Two-factor authentication
  • External login providers (like Google or Facebook)

Prerequisites

Before we begin, ensure you have the following installed:

  • .NET SDK
  • Visual Studio or Visual Studio Code
  • Basic understanding of C# and MVC

Setting Up Your ASP.NET Core MVC Project

Step 1: Create a New Project

  1. Open Visual Studio.
  2. Click on "Create a new project".
  3. Select "ASP.NET Core Web Application" and click "Next".
  4. Name your project (e.g., SecureApp) and click "Create".
  5. Choose the "Web Application (Model-View-Controller)" template and ensure that "Enable Authentication" is set to "Individual User Accounts".

Step 2: Configure Identity in Startup.cs

In the Startup.cs file, ensure that Identity services are configured. You'll find the configuration in the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddDefaultIdentity<IdentityUser>()
        .AddEntityFrameworkStores<ApplicationDbContext>();

    services.AddControllersWithViews();
}

Make sure you have the necessary packages installed, especially for Entity Framework Core and Identity. You can install them via NuGet Package Manager or the command line:

dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Step 3: Update the Database

Next, we need to create a database to store our user data. Run the following commands in the Package Manager Console:

Add-Migration InitialCreate
Update-Database

This will create the necessary database tables for Identity.

Building the Login Functionality

Step 4: Create the Login View

In the Views/Account folder, create a new file named Login.cshtml and add the following code:

@model LoginViewModel

<h2>Login</h2>

<form asp-action="Login" method="post">
    <div class="form-group">
        <label asp-for="Email"></label>
        <input asp-for="Email" class="form-control" />
        <span asp-validation-for="Email" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="Password"></label>
        <input asp-for="Password" class="form-control" />
        <span asp-validation-for="Password" class="text-danger"></span>
    </div>
    <button type="submit" class="btn btn-primary">Login</button>
</form>

Step 5: Create the Login Action

In the AccountController, implement the login action method:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model)
{
    if (ModelState.IsValid)
    {
        var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
        if (result.Succeeded)
        {
            return RedirectToAction("Index", "Home");
        }
        ModelState.AddModelError(string.Empty, "Invalid login attempt.");
    }
    return View(model);
}

Step 6: Create the Login ViewModel

Create a LoginViewModel class in the Models folder:

public class LoginViewModel
{
    [Required]
    [EmailAddress]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    public bool RememberMe { get; set; }
}

Finalizing the Setup

Step 7: Configure Authentication

In the Configure method of Startup.cs, make sure to add authentication:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseAuthentication(); // Add this line
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

Testing Your Application

Now that you have set up the login functionality, run your application:

  1. Press F5 to start debugging.
  2. Navigate to /Account/Login to access the login page.
  3. Register a new user and then try logging in with the credentials.

Conclusion

You have successfully created a secure ASP.NET Core MVC application with Identity Login in a matter of minutes. By following the steps outlined above, you can implement user authentication in your applications, providing a secure experience for your users.

For more advanced features like role management or two-factor authentication, you can refer to the official ASP.NET Core documentation for additional guidance. Happy coding!

Another screenshot from the tutorial
Another view from the tutorial

Connect with SkillBakery Studios

Explore more tutorials, tools, and resources:

Posted by SkillBakery Studios

No comments:

Post a Comment

Post Top Ad