C# – Is a custom role provider in ASP.Net instantiated for every user using Windows Authentication

asp.netasp.net-mvcc

I've written a custom Role Provider for ASP.Net MVC. I'm calling a WCF service to get the roles to authorize the current user. I'm using Windows Authentication.

I want to store the current users roles in a variable for the role provider class but am unclear if the class is created per session, or per application.

Best Answer

It's instantiated once. A quick look at the docs confirmed this:

For each role provider specified in the configuration for an application, ASP.NET instantiates a single role-provider instance that is used for all of the requests served by an HttpApplication object. As a result, you can have multiple requests executing concurrently. ASP.NET does not ensure the thread safety of calls to your provider. You will need to write your provider code to be thread safe. For example, creating a connection to a database or opening a file for editing should be done within the member that is called, such as AddUsersToRoles , rather than opening a file or database connection when the Initialize method is called.