Search code examples
c#asp.netasp.net-membershipclass-libraryasp.net-roles

Is ASP.NET membership, role management and Profile usable in a C# Class Library?


I know it's possible to use this information in a winform, wpf or console application. But I rather to determine which user with what roles are running a sepecific method, so I could decide upon them and run different codes.

In addition in a desktop app. how a user can login? Is there any special winform or wpf login control?


Solution

  • You should be able to use the "principal"; the ASP.NET login sets this up IIRC, and you can do it yourself for winforms, WCF, WPF, etc. You can then use, for example:

    public static bool IsInRole(string role)
    {
        var principal = Thread.CurrentPrincipal;
        return principal == null ? false : principal.IsInRole(role);
    }
    

    You can also get the system to execute the checks for you:

    [PrincipalPermission(SecurityAction.Demand, Role="SuperAdmin")]
    public void DropDatabase() {/* ... */}
    

    From 3.5 (SP1?) onwards, you can use the ASP.NET login mechanism to perform your winform/wpf logins, including setting up a principal; in project properties enable "Enable client application services" (or see the "Learn more" link on that tab).

    Alternatively, writing your own identity/principal is pretty simple - look at IIdentity and IPrincipal ; you don't have to do a lot.