From b5f0302c581c7f98cce8aaab9aaf83e389634912 Mon Sep 17 00:00:00 2001 From: jonne Date: Tue, 19 Nov 2024 19:51:47 -0500 Subject: [PATCH 1/9] extend identity user and add identity to builder services --- Entities/User.cs | 4 +++- Program.cs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Entities/User.cs b/Entities/User.cs index f8bb7d7..1707e6f 100644 --- a/Entities/User.cs +++ b/Entities/User.cs @@ -1,10 +1,12 @@ +using Microsoft.AspNetCore.Identity; + namespace SimpleWebAppReact.Entities; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; /// /// Class structure matches 1-1 with User Table in database /// -public class User +public class User : IdentityUser { [BsonId] [BsonElement("_id"), BsonRepresentation(BsonType.ObjectId)] diff --git a/Program.cs b/Program.cs index 92d5e23..57693d8 100644 --- a/Program.cs +++ b/Program.cs @@ -5,6 +5,7 @@ using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using SimpleWebAppReact; +using SimpleWebAppReact.Entities; using SimpleWebAppReact.Services; var builder = WebApplication.CreateBuilder(args); @@ -47,6 +48,7 @@ }); }); builder.Services.AddSingleton(); +builder.Services.AddIdentity(); builder.Services.AddHttpClient(); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => From 663c4069ad746202ac5a2d4df914b0474c1578a7 Mon Sep 17 00:00:00 2001 From: jonne Date: Tue, 19 Nov 2024 19:53:15 -0500 Subject: [PATCH 2/9] test --- Program.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Program.cs b/Program.cs index 57693d8..366ec96 100644 --- a/Program.cs +++ b/Program.cs @@ -48,6 +48,7 @@ }); }); builder.Services.AddSingleton(); +// Here builder.Services.AddIdentity(); builder.Services.AddHttpClient(); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) From 4dfd8b8e0acac41b99ac13537173014b8eba05ba Mon Sep 17 00:00:00 2001 From: jonne Date: Tue, 19 Nov 2024 20:07:28 -0500 Subject: [PATCH 3/9] add identity to builder --- Entities/User.cs | 4 ++-- Program.cs | 15 +++++++++++++-- SimpleWebAppReact.csproj | 2 ++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Entities/User.cs b/Entities/User.cs index 1707e6f..8eed371 100644 --- a/Entities/User.cs +++ b/Entities/User.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Identity; +using AspNetCore.Identity.MongoDbCore.Models; namespace SimpleWebAppReact.Entities; using MongoDB.Bson; @@ -6,7 +6,7 @@ namespace SimpleWebAppReact.Entities; /// /// Class structure matches 1-1 with User Table in database /// -public class User : IdentityUser +public class User : MongoIdentityUser { [BsonId] [BsonElement("_id"), BsonRepresentation(BsonType.ObjectId)] diff --git a/Program.cs b/Program.cs index 366ec96..6cadd73 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,5 @@ using System.Security.Claims; +using AspNetCore.Identity.MongoDbCore.Models; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; @@ -48,8 +49,18 @@ }); }); builder.Services.AddSingleton(); -// Here -builder.Services.AddIdentity(); +// Here, configure User +var connectionString = builder.Configuration.GetConnectionString("DbConnection"); +var databaseName = builder.Configuration.GetConnectionString("DatabaseName"); +builder.Services.AddIdentity(options => +{ + options.Password.RequireDigit = true; + options.Password.RequireLowercase = true; + options.Password.RequireUppercase = true; + options.Password.RequireNonAlphanumeric = true; + options.Password.RequiredLength = 6; +}).AddMongoDbStores, string>(connectionString, databaseName); + builder.Services.AddHttpClient(); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => diff --git a/SimpleWebAppReact.csproj b/SimpleWebAppReact.csproj index d5839c4..17d636f 100644 --- a/SimpleWebAppReact.csproj +++ b/SimpleWebAppReact.csproj @@ -8,12 +8,14 @@ + + From 43873d0410f6b6b7a7bc05aad9f13187edebab7a Mon Sep 17 00:00:00 2001 From: jonne Date: Tue, 19 Nov 2024 20:33:52 -0500 Subject: [PATCH 4/9] fix adding identity --- Entities/User.cs | 4 ++-- Program.cs | 23 ++++++++++++++--------- SimpleWebAppReact.csproj | 2 +- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Entities/User.cs b/Entities/User.cs index 8eed371..7aafd40 100644 --- a/Entities/User.cs +++ b/Entities/User.cs @@ -1,4 +1,4 @@ -using AspNetCore.Identity.MongoDbCore.Models; +using AspNetCore.Identity.Mongo.Model; namespace SimpleWebAppReact.Entities; using MongoDB.Bson; @@ -6,7 +6,7 @@ namespace SimpleWebAppReact.Entities; /// /// Class structure matches 1-1 with User Table in database /// -public class User : MongoIdentityUser +public class User : MongoUser { [BsonId] [BsonElement("_id"), BsonRepresentation(BsonType.ObjectId)] diff --git a/Program.cs b/Program.cs index 6cadd73..c9c8e80 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,6 @@ using System.Security.Claims; -using AspNetCore.Identity.MongoDbCore.Models; +using AspNetCore.Identity.Mongo; +using AspNetCore.Identity.Mongo.Model; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; @@ -52,14 +53,18 @@ // Here, configure User var connectionString = builder.Configuration.GetConnectionString("DbConnection"); var databaseName = builder.Configuration.GetConnectionString("DatabaseName"); -builder.Services.AddIdentity(options => -{ - options.Password.RequireDigit = true; - options.Password.RequireLowercase = true; - options.Password.RequireUppercase = true; - options.Password.RequireNonAlphanumeric = true; - options.Password.RequiredLength = 6; -}).AddMongoDbStores, string>(connectionString, databaseName); + +// At the ConfigureServices section in Startup.cs +builder.Services.AddIdentityMongoDbProvider(identity => + { + identity.Password.RequiredLength = 8; + // other options + }, + mongo => + { + mongo.ConnectionString = connectionString; + // other options + }); builder.Services.AddHttpClient(); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) diff --git a/SimpleWebAppReact.csproj b/SimpleWebAppReact.csproj index 17d636f..43bd40a 100644 --- a/SimpleWebAppReact.csproj +++ b/SimpleWebAppReact.csproj @@ -8,7 +8,7 @@ - + From bb8d9a0f59eb95d8f84939ebb808826141572f33 Mon Sep 17 00:00:00 2001 From: jonne Date: Tue, 19 Nov 2024 20:44:17 -0500 Subject: [PATCH 5/9] fixed user controller not taking in objectid --- Controllers/UserController.cs | 7 +++++-- Entities/User.cs | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs index a183f88..d98211b 100644 --- a/Controllers/UserController.cs +++ b/Controllers/UserController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using SimpleWebAppReact.Entities; using Microsoft.Extensions.Logging; +using MongoDB.Bson; using MongoDB.Driver; using SimpleWebAppReact.Services; @@ -72,13 +73,14 @@ public async Task> Get([FromQuery] string? name = null, [FromQ [HttpGet("{id}")] public async Task> GetById(string id) { + ObjectId objectId = new ObjectId(id); // Simple validation to check if the ID is not null if (string.IsNullOrEmpty(id)) { return BadRequest("Invalid ID format."); } - var filter = Builders.Filter.Eq(x => x.Id, id); + var filter = Builders.Filter.Eq(x => x.Id, objectId); var user = _users.Find(filter).FirstOrDefault(); return user is not null ? Ok(user) : NotFound(); } @@ -117,7 +119,8 @@ public async Task Update(User user) [HttpDelete("{id}")] public async Task Delete(string id) { - var filter = Builders.Filter.Eq(x => x.Id, id); + ObjectId objectId = new ObjectId(id); + var filter = Builders.Filter.Eq(x => x.Id, objectId); await _users.DeleteOneAsync(filter); return Ok(); } diff --git a/Entities/User.cs b/Entities/User.cs index 7aafd40..9693f9c 100644 --- a/Entities/User.cs +++ b/Entities/User.cs @@ -8,9 +8,9 @@ namespace SimpleWebAppReact.Entities; /// public class User : MongoUser { - [BsonId] - [BsonElement("_id"), BsonRepresentation(BsonType.ObjectId)] - public string? Id { get; set; } + // [BsonId] + // [BsonElement("_id"), BsonRepresentation(BsonType.ObjectId)] + // public string? Id { get; set; } [BsonElement("name"), BsonRepresentation(BsonType.String)] public string? Name { get; set; } From 8efd50c389a5d8ca4250d21a4dea9d1ce1e00d6a Mon Sep 17 00:00:00 2001 From: dpanek27 <143219973+dpanek27@users.noreply.github.com> Date: Tue, 19 Nov 2024 21:54:45 -0500 Subject: [PATCH 6/9] Added role requirements for update, create, and delete events. role must be "RA", "club" or "Greek Life Officer" --- Controllers/EventsController.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Controllers/EventsController.cs b/Controllers/EventsController.cs index 15b7783..e160685 100644 --- a/Controllers/EventsController.cs +++ b/Controllers/EventsController.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SimpleWebAppReact.Entities; using Microsoft.Extensions.Logging; @@ -96,6 +97,8 @@ public async Task> Get([FromQuery] string? eventName = null, /// /// [HttpPost] + //roles that can edit events + [Authorize(Roles ="RA, Club, Greek Life Officer")] public async Task Post(Events events) { await _events.InsertOneAsync(events); @@ -109,6 +112,8 @@ public async Task Post(Events events) /// /// [HttpPut] + //roles that can edit events + [Authorize(Roles ="RA, Club, Greek Life Officer")] public async Task Update(Events events) { var filter = Builders.Filter.Eq(x => x.Id, events.Id); @@ -122,6 +127,8 @@ public async Task Update(Events events) /// /// [HttpDelete("{id}")] + //roles that can edit events + [Authorize(Roles ="RA, Club, Greek Life Officer")] public async Task Delete(string id) { var filter = Builders.Filter.Eq(x => x.Id, id); From 30e4759c149a16eb0035e0ab7647368dee5be5cf Mon Sep 17 00:00:00 2001 From: dpanek27 <143219973+dpanek27@users.noreply.github.com> Date: Thu, 16 Jan 2025 20:52:45 -0500 Subject: [PATCH 7/9] added code to make options.authority "secret" and not appear on github --- Program.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index c9c8e80..26b7624 100644 --- a/Program.cs +++ b/Program.cs @@ -70,7 +70,9 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { - options.Authority = "https://dev-2gowyyl3kin685ua.us.auth0.com/"; + options.Authority = builder.Configuration.GetConnectionString("opt_Authority"); + + //options.Authority = "https://dev-2gowyyl3kin685ua.us.auth0.com/"; options.Audience = "http://localhost:5128"; options.TokenValidationParameters = new TokenValidationParameters { From b32f77220bdb6d4ed9d95c0c4042211e1f2e4ef8 Mon Sep 17 00:00:00 2001 From: dpanek27 <143219973+dpanek27@users.noreply.github.com> Date: Tue, 21 Jan 2025 20:13:07 -0500 Subject: [PATCH 8/9] added secret for options.audience --- Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index 26b7624..86efc2e 100644 --- a/Program.cs +++ b/Program.cs @@ -71,7 +71,7 @@ .AddJwtBearer(options => { options.Authority = builder.Configuration.GetConnectionString("opt_Authority"); - + options.Audience = builder.Configuration.GetConnectionString("opt_audience"); //options.Authority = "https://dev-2gowyyl3kin685ua.us.auth0.com/"; options.Audience = "http://localhost:5128"; options.TokenValidationParameters = new TokenValidationParameters From 439d27f8f032af98a44321a6e86e7b47f0685a2a Mon Sep 17 00:00:00 2001 From: dpanek27 <143219973+dpanek27@users.noreply.github.com> Date: Tue, 21 Jan 2025 20:15:27 -0500 Subject: [PATCH 9/9] opt_audience --- Program.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Program.cs b/Program.cs index 86efc2e..17bc097 100644 --- a/Program.cs +++ b/Program.cs @@ -72,8 +72,6 @@ { options.Authority = builder.Configuration.GetConnectionString("opt_Authority"); options.Audience = builder.Configuration.GetConnectionString("opt_audience"); - //options.Authority = "https://dev-2gowyyl3kin685ua.us.auth0.com/"; - options.Audience = "http://localhost:5128"; options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = ClaimTypes.NameIdentifier,