做科室网站............
之前以为只有一个管理员使用。
就没有设计会员系统。
直接把admin用户放在了credentials节点下....
做完之后。又被告知需要下级单位上传资料。也需要用户名密码。
考虑到不是对外注册且改动性非常小....
就直接还是在credentials节点下保持用户名密码即可。
P.S.由于只内网使用.........直接clear PasswordFormat了..........
->添加新用户
protected void btnRegister_Click(object sender, EventArgs e)
{
FormsAuthenticationUser user = new FormsAuthenticationUser(userName.Text, password.Text);
Configuration configuration = WebConfigurationManager.OpenWebConfiguration("/");
((AuthenticationSection)configuration.GetSection("system.web/authentication")).Forms.Credentials.Users.Add(user);
configuration.Save();
}
创建FormsAuthenticationUser -> 打开web.config -> 定位到credential节点 -> 使用FormsAuthenticationUserCollection的Add方法添加用户 -> 保持web.config
->列表显示所有用户
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
rptUserList.DataSource = (WebConfigurationManager.GetWebApplicationSection("system.web/authentication") as AuthenticationSection).Forms.Credentials.Users;
rptUserList.DataBind();
}
}
绑定集合FormsAuthenticationUserCollection
->删除用户
在上述列表显示用户中同事显示删除按钮
protected void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
string name = e.CommandArgument.ToString();
Configuration configuration = WebConfigurationManager.OpenWebConfiguration("/");
((AuthenticationSection)configuration.GetSection("system.web/authentication")).Forms.Credentials.Users.Remove(name);
configuration.Save();
}
}
-> 用户登录
if (FormsAuthentication.Authenticate(tbName.Text,tbPassword.Text))
{
FormsAuthentication.RedirectFromLoginPage(tbName.Text, true);
}