views:

82

answers:

2

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.

+1  A: 

should not be a app.config file enough?

nWorx
What would you put in the app.config? Normally, the providers reside in the system.web-part of the web.config.
Jonas Lincoln
try this one http://www.theproblemsolver.nl/usingthemembershipproviderinwinforms.htmjust add the membershipprovider in the app.config as you do in web applications in web.config
nWorx
Excellent. That solved it. I didn't know that console apps cared about system.web-config sections, but it turned out to be real easy.
Jonas Lincoln
A: 

Seems like you have not configured your Membership-provider in your web.config correctly. Can you post the section of your config where the provider is stated?

Mattias