从控制台应用程序以编程方式设置角色
我正在为我们的网络应用程序编写一个设置应用程序。其中一项任务是为内置 SqlRoleProvider 设置数据库。我已经有了使用 aspnet_regsql.exe
命令创建数据库的脚本,但现在我在以编程方式创建默认角色和用户映射时遇到了麻烦。
我想做的:
...
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);
}
...
问题是,运行此程序时,我收到一个异常,提示“角色管理器未激活”,这当然是正确的,因为通常角色仅从 Web 应用程序处理。
我已经通过直接执行内置存储过程来解决这个问题,但我很好奇如何以编程方式解决这个问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
app.config 文件不应该足够吗?
should not be a app.config file enough?
似乎您没有在 web.config 中正确配置您的会员提供程序。您可以发布说明提供程序的配置部分吗?
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?