Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, good for you for not using a membership provider. That just doesn't work well with OpenID.</p> <p>To make roles work without a membership provider, you need to implement your own class that derives from <code>System.Web.Security.RoleProvider</code>. It's completely departed from authentication, which makes it easy for you. You just need to store with each of your users in your database which roles they belong to, and then your RoleProvider interacts with that database.</p> <p>Once you write your role provider class, wire it up with this in your web.config file. This snippet should appear within your <code>system.web</code> section.</p> <pre><code>&lt;roleManager enabled="true" defaultProvider="Database"&gt; &lt;providers&gt; &lt;add name="Database" type="MyRoleProvider" /&gt; &lt;/providers&gt; &lt;/roleManager&gt; </code></pre> <p>Here's one role provider I wrote for an OpenID web application. It's written using Linq to Entities, but you can get the idea and implement it to work against your database.</p> <pre><code>public class MyRoleProvider : RoleProvider { public override string ApplicationName { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } public override void AddUsersToRoles(string[] usernames, string[] roleNames) { var users = from token in Global.DataContext.AuthenticationToken where usernames.Contains(token.ClaimedIdentifier) select token.User; var roles = from role in Global.DataContext.Role where roleNames.Contains(role.Name, StringComparer.OrdinalIgnoreCase) select role; foreach (User user in users) { foreach (Role role in roles) { user.Roles.Add(role); } } } public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { var users = from token in Global.DataContext.AuthenticationToken where usernames.Contains(token.ClaimedIdentifier) select token.User; var roles = from role in Global.DataContext.Role where roleNames.Contains(role.Name, StringComparer.OrdinalIgnoreCase) select role; foreach (User user in users) { foreach (Role role in roles) { user.Roles.Remove(role); } } } public override void CreateRole(string roleName) { Global.DataContext.AddToRole(new Role { Name = roleName }); } /// &lt;summary&gt; /// Removes a role from the data source for the configured applicationName. /// &lt;/summary&gt; /// &lt;param name="roleName"&gt;The name of the role to delete.&lt;/param&gt; /// &lt;param name="throwOnPopulatedRole"&gt;If true, throw an exception if &lt;paramref name="roleName"/&gt; has one or more members and do not delete &lt;paramref name="roleName"/&gt;.&lt;/param&gt; /// &lt;returns&gt; /// true if the role was successfully deleted; otherwise, false. /// &lt;/returns&gt; public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { Role role = Global.DataContext.Role.SingleOrDefault(r =&gt; r.Name == roleName); if (role == null) { return false; } if (throwOnPopulatedRole &amp;&amp; role.Users.Count &gt; 0) { throw new InvalidOperationException(); } Global.DataContext.DeleteObject(roleName); return true; } /// &lt;summary&gt; /// Gets an array of user names in a role where the user name contains the specified user name to match. /// &lt;/summary&gt; /// &lt;param name="roleName"&gt;The role to search in.&lt;/param&gt; /// &lt;param name="usernameToMatch"&gt;The user name to search for.&lt;/param&gt; /// &lt;returns&gt; /// A string array containing the names of all the users where the user name matches &lt;paramref name="usernameToMatch"/&gt; and the user is a member of the specified role. /// &lt;/returns&gt; public override string[] FindUsersInRole(string roleName, string usernameToMatch) { return (from role in Global.DataContext.Role where role.Name == roleName from user in role.Users from authTokens in user.AuthenticationTokens where authTokens.ClaimedIdentifier == usernameToMatch select authTokens.ClaimedIdentifier).ToArray(); } public override string[] GetAllRoles() { return Global.DataContext.Role.Select(role =&gt; role.Name).ToArray(); } public override string[] GetRolesForUser(string username) { return (from authToken in Global.DataContext.AuthenticationToken where authToken.ClaimedIdentifier == username from role in authToken.User.Roles select role.Name).ToArray(); } public override string[] GetUsersInRole(string roleName) { return (from role in Global.DataContext.Role where string.Equals(role.Name, roleName, StringComparison.OrdinalIgnoreCase) from user in role.Users from token in user.AuthenticationTokens select token.ClaimedIdentifier).ToArray(); } public override bool IsUserInRole(string username, string roleName) { Role role = Global.DataContext.Role.SingleOrDefault(r =&gt; string.Equals(r.Name, roleName, StringComparison.OrdinalIgnoreCase)); if (role != null) { return role.Users.Any(user =&gt; user.AuthenticationTokens.Any(token =&gt; token.ClaimedIdentifier == username)); } return false; } public override bool RoleExists(string roleName) { return Global.DataContext.Role.Any(role =&gt; string.Equals(role.Name, roleName, StringComparison.OrdinalIgnoreCase)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload