Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Check if the MinimumAgeAuthorizeAttribute Could Be Simplified #29836

Unanswered
sophart asked this question in Q&A
Discussion options

Code from documentation here.

I'm not sure if I'm right but I just want to know if we can do this.
Instead of this:

 internal class MinimumAgeAuthorizeAttribute : AuthorizeAttribute
{
 const string POLICY_PREFIX = "MinimumAge";
 public MinimumAgeAuthorizeAttribute(int age) => Age = age;
 // Get or set the Age property by manipulating the underlying Policy property
 public int Age
 {
 get
 {
 if (int.TryParse(Policy.Substring(POLICY_PREFIX.Length), out var age))
 {
 return age;
 }
 return default(int);
 }
 set
 {
 Policy = $"{POLICY_PREFIX}{value.ToString()}";
 }
 }

Can we just do this?

 public class MinimumAgeAuthorizeAttribute : AuthorizeAttribute
 {
 const string POLICY_PREFIX = "MinimumAge";
 private readonly int _age;
 public MinimumAgeAuthorizeAttribute(int age)
 {
 _age = age;
 Policy = $"{POLICY_PREFIX}{age}";
 }
 public int Age => _age;
 }
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /