can i set a role description when adding a new role
When adding a role (making use of asp.net's role provider), i cannot find a method to add a role including a description.
the asp.net Role table in SQL makes provision for a role description, however the only method available is:
开发者_开发百科Roles.CreateRole(string RoleName);
there is no overload to include a description.
Is there an out of the box way of including the description? or should i go about this myself?
As you probably found out by now there's no direct way of getting hold of the description. It's only there because it's a member of the BaseProvider. Now it's fairly simple to make your own method for adding support for the description. You cannot extend the Roles class though but you can do something like this:
public static class RolesEx
{
public static void CreateRole(string roleName, string description)
{
Roles.CreateRole(roleName);
var c = new SqlConnection("connString");
var cmd = c.CreateCommand();
cmd.CommandText =
string.Format(
"UPDATE aspnet_Roles SET Description = '{0}' WHERE ApplicationId = (SELECT ApplicationId FROM aspnet_Applications WHERE LoweredApplicationName = '{1}') AND LoweredRoleName = '{2}'",
description, Roles.ApplicationName.ToLower(), roleName.ToLower());
cmd.CommandType = CommandType.Text;
c.Open();
var i = cmd.ExecuteNonQuery();
c.Close();
}
}
My apologies if the code looks messy. This is my first post here o.O´
精彩评论