Refactoring long if statement that checks for user roles
Hi, I'm trying to find a cleaner way of refactoring this if code block that checks for each user role. The if statement works but looks really messy, and I was wondering if it's possible to do something like how EF Core would do, just check if it contains something using
.Contains()
.
A
Angius•511d ago
I'd probably create an extension method on
User
Or create some authorization serviceA
Anton•511d ago
new[]{"Role1",...}.Any(User.IsInRole)
you can move the roles into a static readonly field to make sure it's not allocated multiple timesC
CoreVisional•511d ago
Hmm, extension method?
A
Angius•511d ago
Yeah
In case you didn't know what extension methods are...
M
MODiX•511d ago
C
CoreVisional•511d ago
I'm still confused as in how does this relate to checking for the roles.
A
Angius•510d ago
You asked what extension methods are... well, this is what they are
You can declare one on your
User
that has all those checks inside
So that you can just do if (User.CustomChecks())
E
ero•510d ago
Or just a computed property on the user
A
Angius•510d ago
User
in this instance is of, I believe, IClaimsPrincipal
, type
It's not an IdentityUser
Yeah, the naming is a bit confusing, but it is what it isC
CoreVisional•510d ago
Ahh okay, I went and try to create the custom extension check in one razor view.... so i was stuck trying to figure out how to do it
Wait, so it's something like
public static bool (this IPrincipal user)
and not public static bool (this User user)
?A
Angius•510d ago
ye
C
CoreVisional•510d ago
Does
IsInRole
checks for the entire role name or part of it? Like, if I just specify "Department", does it verify all "Department.A", "Department.B", etc?
how to check for true : false here btw? I'm looking to return false if the second variable is true for example.
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
51KMembers
View on DiscordWant results from more Discord servers?
More Posts❔ IEnumerable and dependency injectionif i want to loop enumerable from DI multiple times
```
var foos = services.GetRequiredService<IEnum❔ How did you learn . NET ?Books /youtube/udemy /other courses, what resources made you learn .NET to use as your job as web de✅ Is it possible to disable these properties on EF6? Web API 2 project, multi-layered, NF472SCENARIO
- Solution type is NET Framework 4.7.2 (it is, what it is).
- I have this project, I'm cons✅ This EntityFramework query can not be translated for SQLiteThe issue lies within `Intersect`. The point of the query is to fetch the current user, and to popul✅ Detect Network RequestI want make service that detect all connection to specific domain or ip?
And if this request contain❔ CORS errorHello, I have Cors error on my project. I add Addpolicy in my configureServices and also add app.Us❔ Ambiguities? How?Idk how i can resolve this i don't see the problem✅ How can I get rid of this annoying pointer thing in the first row, datagridview winforms^✅ How can I use a row header of a datagridview to display the row number instead of being empty?^✅ How can i properly display this datagridviewHow can i get rid of the row select thing on the left (just white space)✅ I am trying to output a 2D array to a datagridview, but it is not outputting correctlyIt is saying System.Int32[]✅ Task not returning and stopping programHey,
I am currently trying to start one task for every string in the list, but this isn't working an❔ DbContextCan i use this in a non-asp area like for example, a simple program to make few tables using DbSet✅ The specified deps.json does not exist while scaffolding DBI am going through a tutorial to create a Web API app with ASP.NET6 and MySql. I wanted to scaffold ❔ C sharp object reference errorI am trying to reference an object that I'm displaying on PageHeader.xaml and I keep getting this erInteroperability issues (PInvoke)Hi!
I'm calling a native CPP library and retrieving a struct by reference which returns a struct con❔ SKIA SHARP PROBLEMError:
```cmd
Dec 02 16:05:22 mourashop shopmoura-api-app[3847595]: Unhandled exception. System.Argu❔ FluentAssertions excluding collection elementsI have two IEnumerable<Claim> tmp1, tmp2 and I want to see if they are equal, except for the claims ✅ Best way to display a 2D arrayWhat would be the best way to display a 2D array in winforms c#, with the best design, or is just prWrong count for my inversions functionIt is supposed to be 10 inversion count but I am getting 13
```csharp
var array = new int