Hi @S Abijith , Welcome to Microsoft Q&A,
You can use Windows Authentication to get the currently logged in Windows user. You can use Environment.UserName or WindowsIdentity.GetCurrent() to get the username of the currently logged in user.
using System.Security.Principal;
string currentUser = Environment.UserName;
// or
WindowsIdentity identity = WindowsIdentity.GetCurrent();
string userName = identity.Name;
Windows user roles can be managed through Windows groups. Using WindowsPrincipal and WindowsIdentity, you can determine whether a user belongs to certain Windows groups, such as the Administrators group. Here is the sample code:
using System.Security.Principal;
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
// Check if the user is an administrator
bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
// You can use a custom role name to check if it belongs to a specific group
bool isSpecificRole = principal.IsInRole("YourCustomRoleName");
Based on the obtained user role or Windows group information, you can restrict access to various functions of the application.
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.