0% found this document useful (0 votes)
5 views5 pages

Customer App Backend Setup Guide

The document outlines the backend implementation of a customer application using ASP.NET Core, including database setup, JWT authentication, and API endpoints for user login and customer management. It utilizes Entity Framework for database interactions and includes middleware for authentication and authorization. The application supports basic CRUD operations for customers and demonstrates a simple user authentication system with hardcoded demo users.

Uploaded by

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

Customer App Backend Setup Guide

The document outlines the backend implementation of a customer application using ASP.NET Core, including database setup, JWT authentication, and API endpoints for user login and customer management. It utilizes Entity Framework for database interactions and includes middleware for authentication and authorization. The application supports basic CRUD operations for customers and demonstrates a simple user authentication system with hardcoded demo users.

Uploaded by

carewelloman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Customer Application BACKEND — Program.

cs (ALL-IN-ONE)

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

var builder = [Link](args);

// ---------- DB ----------

[Link]<AppDbContext>(o =>

[Link]("Server=.;Database=CustomerDB;Trusted_Connection=True;TrustServ
erCertificate=True;"));

// ---------- JWT ----------

var jwtKey = "SUPER_SECRET_KEY_123456789";

[Link]([Link])

.AddJwtBearer(o =>

[Link] = new TokenValidationParameters

ValidateIssuer = false,

ValidateAudience = false,

ValidateIssuerSigningKey = true,

IssuerSigningKey = new SymmetricSecurityKey([Link](jwtKey))

};

});
Customer Application BACKEND — [Link] (ALL-IN-ONE)

[Link]();

[Link]();

[Link]();

[Link]();

var app = [Link]();

// ---------- MIDDLEWARE ----------

[Link]();

[Link]();

[Link](x => [Link]().AllowAnyHeader().AllowAnyMethod());

[Link]();

[Link]();

// ---------- AUTO DB ----------

using (var scope = [Link]())

var db = [Link]<AppDbContext>();

[Link]();

// ---------- USERS (DEMO) ----------

var users = new[]

new AppUser("admin","admin123","Admin"),

new AppUser("user","user123","User")
Customer Application BACKEND — [Link] (ALL-IN-ONE)

};

// ---------- LOGIN ----------

[Link]("/login", (LoginReq req) =>

var u = [Link](x => [Link] == [Link] && [Link] ==


[Link]);

if (u == null) return [Link]();

var claims = new[]

new Claim([Link], [Link]),

new Claim([Link], [Link])

};

var token = new JwtSecurityToken(

claims: claims,

expires: [Link](1),

signingCredentials: new SigningCredentials(

new SymmetricSecurityKey([Link](jwtKey)),

SecurityAlgorithms.HmacSha256)

);

return [Link](new

token = new JwtSecurityTokenHandler().WriteToken(token),

role = [Link]

});
Customer Application BACKEND — [Link] (ALL-IN-ONE)

});

// ---------- CUSTOMER APIs ----------

[Link]("/customers", async (AppDbContext db) =>

await [Link]()

).RequireAuthorization();

[Link]("/customers", async (Customer c, AppDbContext db) =>

[Link](c);

await [Link]();

return [Link](c);

}).RequireAuthorization();

[Link]("/customers/{id:int}", async (int id, AppDbContext db) =>

var c = await [Link](id);

if (c == null) return [Link]();

[Link](c);

await [Link]();

return [Link]();

}).RequireAuthorization("Admin");

[Link]();

// ---------- MODELS ----------

record LoginReq(string Username, string Password);

record AppUser(string Username, string Password, string Role);


Customer Application BACKEND — [Link] (ALL-IN-ONE)

class Customer

public int CustomerId { get; set; }

public string FullName { get; set; }

public string? Email { get; set; }

class AppDbContext : DbContext

public AppDbContext(DbContextOptions<AppDbContext> o) : base(o) { }

public DbSet<Customer> Customers => Set<Customer>();

You might also like