I'm writing a setup-application for our web application. One of the tasks is to setup the database for the built-in SqlRoleProvider. I've got my script to create the database with the aspnet_regsql.exe
-command, but now I've run into troubles creating default roles and user-mappings programatically.
What I'd like to do:
...
private class UserRole
{
public string Username { get; set; }
public string Role { get; set; }
}
...
const string applicationName = "foo";
var roles = new List<string> { "Administrator", "Editor" };
var userRoles =
new List<UserRole>
{
new UserRole {Username = "joli", Role = "Administrator"},
new UserRole {Username = "test", Role = "Editor"}
};
Roles.ApplicationName = applicationName;
foreach (var userRole in userRoles)
{
Roles.AddUserToRole(userRole.Username, userRole.Role);
}
...
The problem is that when running this, I get an exception saying "The rolemanager is not activated", which of course is correct because normally Roles are only handled from a web application.
I've made a workaround for this with executing the built-in stored procedures directly, but I'm curious on how to solve this programmatically.