Enabling Winforms Application Windows authentication service

Aditya Bhattacharjee 0 Reputation points
2024-06-09T08:49:58.2+00:00

Screenshot 2024-06-09 141350

Hi,

I want to add windows authentication to my Winforms Application. I have gone to properties and enabled this as shown in the screenshot. Want to test if its working or not. How do i proceed?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,876 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2024-06-10T06:21:33.88+00:00

    Hi @Aditya Bhattacharjee , Welcome to Microsoft Q&A,

    Modify App.config or web.config (for .NET Framework)

    If your application has an App.config file, make sure it contains the following settings:

    <configuration>
    <system.web>
    <authentication mode="Windows" />
    <authorization>
    <deny users="?" />
    </authorization>
    </system.web>
    </configuration>
    
    

    Using Windows Authentication in Code In your WinForms application code, you can use the WindowsIdentity class to get the identity information of the current user. You can add the following code in the Form_Load event to test authentication:

    using System;
    using System.Security.Principal;
    using System.Windows.Forms;
    
    namespace YourNamespace {
        public partial class YourForm: Form {
            public YourForm() {
                InitializeComponent();
            }
    
            private void YourForm_Load(object sender, EventArgs e) {
                // Get the current Windows identity
                WindowsIdentity identity = WindowsIdentity.GetCurrent();
                if (identity != null) {
                    // Display current user information
                    MessageBox.Show($ "Current user: {identity.Name}");
                } else {
                    MessageBox.Show("Unable to get the current user identity.");
                }
            }
        }
    }
    

    If you need to perform specific actions based on the user identity, you can further check for user groups or specific users in your code. For example:

    using System.Security.Principal;
    
    private void YourForm_Load(object sender, EventArgs e) {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(identity);
    
        // Check if the user is in the administrator group
        if (principal.IsInRole(WindowsBuiltInRole.Administrator)) {
            MessageBox.Show("You are an administrator.");
        } else {
            MessageBox.Show("You are not an administrator.");
        }
    }
    

    You can also refer to: How to use Windows Authentication in Windows 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.

    1 person found this answer helpful.
    0 comments No comments