How can user permissions be created?

fatih uyanık 80 Reputation points
2023-05-18T06:58:25.86+00:00

Hello

A friend asked me. I wanted to write in case I might get a different suggestion.

Working on wpf project. In the project, there are users who log in to the system by entering a password at the beginning. What he wants to do is to assign roles to these users, such as admin, super admin and user. Then, when the administrator adds a user to the system or wants to give administrator permission, he needs to get permission from the super administrator. How to set up these groups, permissions and permissions scenario?

Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 47,336 Reputation points Microsoft Vendor
    2023-05-18T09:13:51.1566667+00:00

    Hi,@fatih uyanık. Welcome Microsoft Q&A.

    For setting permissions such as administrators and ordinary users, you could refer to the solutions here and here. For each user, there is a permission option property, and the user's role can be changed by setting the value of the attribute.


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. don bradman 621 Reputation points
    2023-05-18T13:28:04.7933333+00:00

    To set up user roles, you can use the System.Web.Security.Roles class. This class provides methods for creating, deleting, and managing roles. You can create roles using the Roles.CreateRole method and delete roles using the Roles.DeleteRole method. You can also add users to roles using the Roles.AddUserToRole method and remove users from roles using the Roles.RemoveUserFromRole method.

    Example:

    if (!Roles.RoleExists("Admin"))
    {
    Roles.CreateRole("Admin");
    }
    

    To set up permissions, you can use the System.Security.Principal.WindowsPrincipal class. This class provides methods for checking whether a user has a specific permission. You can use the WindowsPrincipal.IsInRole method to check whether a user is in a specific role.

    Example:

    WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    if (principal.IsInRole("Admin"))
    {
    // User has admin permission
    }
    

    To set up permission scenarios, you can use a combination of user roles and permissions. For example, you can create a "Super Admin" role and only allow users in that role to add new users or give administrator permission. You can also use the WindowsPrincipal.IsInRole method to check whether a user is in the "Super Admin" role before allowing them to perform these actions.

    Example:

    WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    if (principal.IsInRole("Super Admin"))
    {
    // User has super admin permission
    }
    

    You can also use the AuthorizeAttribute class to restrict access to specific actions or controllers based on user roles. This class allows you to specify which roles are allowed to access a particular action or controller.

    Example:

    [Authorize(Roles = "Admin")]
    public ActionResult AddUser()
    {
        // Only users in the "Admin" role can access this action
    }
    
    0 comments No comments