Home > Back-end >  User authorization for C# Winform applications (not ASP.NET)
User authorization for C# Winform applications (not ASP.NET)

Time:03-16

I would like to restrict certain features in a C# Winform application to specific users / user groups. I can see lots of examples using ASP.NET but very little information about how to do it for C# WInform applications. Any recommendations?

CodePudding user response:

If your idea is to limit the features you could try to do this: combine using the .enable/.isEnabled function that every obj in a winform has to make it usable or clickable by the user, with the .visibility command to change the interface in base of the type of user priviledge.

public class User()
{
    public string Username { get; set; }
    public string Password { get; set; }
    public bool IsAdministrator { get; set; }

    public User() { }
}

Then when the user fill the form and press "login" for example you do something like:

User activeUser = new User();

activeUser.Username = nameTextbox.Text //ecc ecc for the psw

If(activeuser.IsAdministrator)
{
    buttonexample.Enabled = true;
    buttonexample.visible = true;

    otherbutton.Enabled = false;
    otherbutton.visible = false;

    // insert other commands for interface control
}

Other that this try to move around using different forms for different tasks, this will help break up the code to make it easier to work with.
A plausible structure could be:

  • Login page (connected to a list where you have the user privileges defined for every user)
  • Main interface where you hide & show only the features you want
  • Maybe some others forms for user creation/change password/ecc

If you're instead asking for an actual authorization helping library or something else maybe rephrase the question a bit.

  • Related