Click here to Skip to main content
15,892,072 members
Articles / Web Development / IIS

Custom MembershipProvider and RoleProvider Implementations that use Web Services

Rate me:
Please Sign up or sign in to vote.
4.70/5 (85 votes)
4 Oct 2009CPOL7 min read 1M   8.3K   251  
Custom MembershipProvider and RoleProvider implementations that use web services in order to separate the application and database servers.
using System;
using System.Web;
using System.Collections;
using System.Web.UI;
using System.Web.Services;
using System.Web.Services.Protocols;


/// <summary>
/// Summary description for RoleProvider
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class RoleProvider : System.Web.Services.WebService
{
  protected System.Web.Security.RoleProvider GetProvider(string providerName, string applicationName)
  {
    System.Web.Security.RoleProvider provider;
    if ((providerName != null) && (System.Web.Security.Roles.Providers[providerName] != null))
    {
      provider = System.Web.Security.Roles.Providers[providerName];
    }
    else
    {
      provider = System.Web.Security.Roles.Provider;
    }

    if (applicationName != null)
    {
      provider.ApplicationName = applicationName;
    }

    return provider;
  }

  public RoleProvider()
  {
  }

  [WebMethod(Description="")]
  public void AddUsersToRoles(string providerName, string applicationName, string[] usernames, string[] roleNames)
  {
    GetProvider(providerName, applicationName).AddUsersToRoles(usernames, roleNames);
  }

  [WebMethod(Description = "")]
  public void CreateRole(string providerName, string applicationName, string roleName)
  {
    GetProvider(providerName, applicationName).CreateRole(roleName);
  }

  [WebMethod(Description = "")]
  public bool DeleteRole(string providerName, string applicationName, string roleName, bool throwOnPopulatedRole)
  {
    return GetProvider(providerName, applicationName).DeleteRole(roleName, throwOnPopulatedRole);
  }

  [WebMethod(Description = "")]
  public string[] FindUsersInRole(string providerName, string applicationName, string roleName, string usernameToMatch)
  {
    return GetProvider(providerName, applicationName).FindUsersInRole(roleName, usernameToMatch);
  }

  [WebMethod(Description = "")]
  public string[] GetAllRoles(string providerName, string applicationName)
  {
    return GetProvider(providerName, applicationName).GetAllRoles();
  }

  [WebMethod(Description = "")]
  public string[] GetRolesForUser(string providerName, string applicationName, string username)
  {
    return GetProvider(providerName, applicationName).GetRolesForUser(username);
  }

  [WebMethod(Description = "")]
  public string[] GetUsersInRole(string providerName, string applicationName, string roleName)
  {
    return GetProvider(providerName, applicationName).GetUsersInRole(roleName);
  }

  [WebMethod(Description = "")]
  public bool IsUserInRole(string providerName, string applicationName, string username, string roleName)
  {
    return GetProvider(providerName, applicationName).IsUserInRole(username, roleName);
  }

  [WebMethod(Description = "")]
  public void RemoveUsersFromRoles(string providerName, string applicationName, string[] usernames, string[] roleNames)
  {
    GetProvider(providerName, applicationName).RemoveUsersFromRoles(usernames, roleNames);
  }

  [WebMethod(Description = "")]
  public bool RoleExists(string providerName, string applicationName, string roleName)
  {
    return GetProvider(providerName, applicationName).RoleExists(roleName);
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Employed (other) Purplebricks
Australia Australia
All articles are supplied as-is, as a howto on a particular task that worked for me in the past. None of the articles are supposed to be out-of-the-box freeware controls and nor should they be treated as such. Caveat emptor.

Now living and working in Australia, trying to be involved in the local .NET and Agile communities when I can.

I spend a good chunk of my spare time building OpenCover and maintaining PartCover both of which are Code Coverage utilities for .NET.

Comments and Discussions