Create and delete roles

Source: Internet
Author: User
Tags connectionstrings

Settings in Web. config
<Configuration>
<Connectionstrings>
<Add name ="Sqlservices"Connectionstring =" Data Source = wangxiaojun; initial catalog = aspnetdb; Integrated Security = true"
/>
</Connectionstrings>

<Authentication mode ="Forms">
<Forms loginurl = "default. aspx"> </Forms>
</Authentication>

<Rolemanager enabled = "true" defaultprovider = "sqlprovider">
<Providers>
<Add name = "sqlprovider" type = "system. Web. Security. sqlroleprovider"
Connectionstringname ="Sqlservices"/>
</Providers>
</Rolemanager>

Addrole. ASPX pageCode

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "addroles. aspx. cs" inherits = "addroles" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Table border = "0" cellpadding = "2" cellspacing = "2" class = "Main">
<Tr align = "center" class = "head">
<TD> <B> role management </B> </TD>
</Tr>
<Tr>
<TD align = "center">
<Asp: textbox id = "txtrole" runat = "server" width = "120px"> </ASP: textbox>
& Nbsp
<Asp: button id = "btnsubmit" runat = "server" text = "OK" onclick = "btnsubmit_click"/>
<HR/>
</TD>
</Tr>
<Tr>
<TD align = "center">
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" gridlines = "NONE"
Font-size = "small" width = "204px">
<Columns>
<Asp: templatefield headertext = "role name">
<Itemtemplate>
<Asp: Label id = "lbrolename" runat = "server" forecolor = "black"
TEXT = '<% # container. dataitem. tostring () %>' > </ASP: Label>
</Itemtemplate>
<Headerstyle Height = "25px"/>
</ASP: templatefield>
<Asp: templatefield>
<Itemtemplate>
<Asp: linkbutton id = "lbedit" runat = "server" text = "Edit role"
Commandname = "editrole" commandargument = '<% # container. dataitem. tostring () %>'
Oncommand = "linkbuttonclick" Forecolor = "blue"> </ASP: linkbutton>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield>
<Itemtemplate>
<Asp: linkbutton id = "lbdel" runat = "server" text = "delete" commandname = "deleterole"
Commandargument = '<% # container. dataitem. tostring () %> 'oncommand = "linkbuttonclick"
Forecolor = "blue" Onclientclick = "Return confirm ('Are you sure you want to delete this role? '); "> </ASP: linkbutton>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>
</TD>
</Tr>
<Asp: Panel Id = "plusers" runat = "server" visible = "false">
<Tr>
<TD Align = "center">
<HR/>
<Asp: gridview id = "gvusers" runat = "server" autogeneratecolumns = "false" width = "100%"
Gridlines = "NONE" font-size = "small">
<Columns>
<Asp: templatefield headertext = "User Name">
<Itemtemplate>
<Asp: Label id = "lbusername" runat = "server" forecolor = "black"
TEXT = '<% # databinder. eval (container. dataitem, "username") %>' >
</ASP: Label>
</Itemtemplate>
<Headerstyle Height = "25px"/>
</ASP: templatefield>
<Asp: templatefield headertext = "role?">
<Itemtemplate>
<Asp: checkbox id = "cbuserinrole" runat = "server" autopostback = "true"
Forecolor = "blue" oncheckedchanged = "checkbox_click"
Tooltip = '<% # databinder. eval (container. dataitem, "username") %>' />
</Itemtemplate>
<Headerstyle Height = "25px"/>
</ASP: templatefield>
</Columns>
</ASP: gridview>
</TD>
</Tr>
</ASP: Panel>
<Tr>
<TD align = "center">
<Asp: Label id = "lbmessage" runat = "server" forecolor = "red">
</ASP: Label>
</TD>
</Tr>
<Tr>
<TD class = "head" align = "right" style = "height: 23px">
<A href = "adduser. aspx"> set roles for users </a>
</TD>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>

Addroles. aspx. CS Page code

using system;
using system. data;
using system. configuration;
using system. collections;
using system. web;
using system. web. security;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. webcontrols. webparts;
using system. web. UI. htmlcontrols;

Public partial class addroles: system. web. UI. page
{< br> protected void page_load (Object sender, eventargs e)
{< br> If (! Ispostback)
{< br> gridview1.datasource = roles. getallroles ();
gridview1.databind ();
gvusers. datasource = membership. getallusers ();
gvusers. databind ();
}< BR >}

protected void linkbuttonclick (Object sender, commandeventargs E)
{< br> If ( E. commandname. equals ("editrole")
{< br> string rname = E. commandargument. tostring ();
gvusers. caption = "set role " + rname + " Users";

for (INT I = 0; I {< br> checkbox checkb1 = (checkbox) gvusers. rows [I]. findcontrol ("cbuserinrole");
string uname = checkb1.tooltip;
checkb1.checked = roles. isuserinrole (uname, rname);
checkb1.attributes ["role"] = rname;
}

Plusers. Visible = true;
}

If (E. commandname. Equals ("deleterole "))
{
String deleter = E. commandargument. tostring ();
Int tleng = roles. getusersinrole (deleter). length;

If (tleng! = 0)
{
Lbmessage. Text = "this role also contains users and cannot be deleted ";
Return;
}

If (roles. deleterole (deleter ))
{
Gridview1.datasource = roles. getallroles ();
Gridview1.databind ();
Lbmessage. Text = "deleted ";
}
Else
{
Lbmessage. Text = "failed to delete ";
}
}
} // Linkbuttonclick

Protected void btnsubmit_click (Object sender, eventargs E)
{
String createrole = txtrole. text;

If (txtrole. Text = string. Empty)
{
Lbmessage. Text = "Enter the role name ";
Return;
}

If (! Roles. roleexists (createrole)
{< br> roles. createrole (createrole);
gridview1.datasource = roles. getallroles ();
gridview1.databind ();
lbmessage. TEXT = "added successfully";
}< br> else
{< br> lbmessage. TEXT = "the role exists. Please enter it again";
}< BR >}// btnsubmit_click

Protected void checkbox_click (Object sender, eventargs E)
{
Try
{
Checkbox cbuserinrole = (checkbox) sender;
String uname = cbuserinrole. tooltip;
String rname = cbuserinrole. attributes ["role"];

If (! Cbuserinrole. Checked)
{
Roles. removeuserfromrole (uname, rname );

}
Else
{
Roles. addusertorole (uname, rname );
}

Lbmessage. Text = "updated successfully ";
} // Try
Catch (system. configuration. provider. providerexception ex)
{
Lbmessage. Text = ex. message;
}

}

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.