Windows Session Authentication

S Abijith 471 Reputation points
2024-11-07T09:01:18.4466667+00:00

Hello All,

We have a Windows application built on .Net Framework 4.8. This application provides a login window was until now being handled by an SQLite database.

But now we would want to implement 'Windows Session Management'. We must also get the role of the logged in Windows user as we have a few features that must be available for only for a particular user type.

Can anyone help us on this??

Any help is highly appreciated. Thank you in advance!!

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-11-07T09:57:56.7966667+00:00

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.