Saving static user data in a C # Windows application

I have an application that needs to get into ActiveDirectory in order to get user / role rights when the application starts and persist everywhere.

I don’t want to remove AD on each form in order to double-check user permissions, so I would like for the user role and, possibly, other user login information to be available worldwide in any form of the application, so I can correctly hide the functionality, buttons, etc., if necessary.

Sort of:

if (UserProperties.Role == Roles.Admin)
{
    btnDelete.Visible = false;
}

What are the best methods for storing static user data in a Windows application? Solutions like Singleton or global variables may work, but I tried to avoid them.

Is the User object that is passed to each form constructor just as bad?

+3
source share
4 answers

Set Thread.CurrentPrincipal using WindowsPrincipal , a GenericPrincipal, or your custom principle. Then you can just call IsInRole :

if (Thread.CurrentPrincipal.IsInRole(Roles.Admin)) {
   btnDelete.Visible = false;
}
+4
source

Perhaps my judgment is overshadowed by my frequent use of javascript, but I think that if you have something that means global, then using global variables is fine.

, , . , .

+2

( ) , ( AppDomain).

However, given that you are talking about actually caching user security credentials, you might need to think carefully about security loopholes. For example, what happens if a user leaves an application that has been running for several days? They can perform transactions under their long-standing credentials, and not with their most modern credentials. Depending on what you provide, you might be better off checking credentials on demand or at least periodically expiring cached credentials.

0
source

Source: https://habr.com/ru/post/1699293/


All Articles