开发者

Setup roles programmatically from a console application

I'm writing a setup-application for our web app开发者_如何学Golication. 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.


should not be a app.config file enough?


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜