这种方法修改web.config可不可以
这种方法修改web.config可不可以
void btnLogin_Click(Object sender, EventArgs e)
{
XmlDocument webconfigDoc = new XmlDocument();
string filePath = HttpContext.Current.Request.PhysicalApplicationPath + @"\web.config"; //设置节的xml路径
string xPath = "/configuration/authentication/forms[@user='password']";
//加载web.config文件
webconfigDoc.Load(filePath);
//找到要修改的节点
XmlNode passkey = webconfigDoc.SelectSingleNode(xPath.Replace("user","password"));
//设置节点的值
string textdd = txtUsername.Text;
passkey.Attributes["password"].InnerText = textdd;
//保存设置
webconfigDoc.Save(filePath);
}
web.config里的代码如下
<system.web>
<compilation debug="true"/>
<authentication mode="Forms" >
<forms loginUrl="/admin/login.aspx" protection="All" name=".AdminNameAuth">
<credentials passwordFormat="Clear">
<user name="admin" password="10000"/>
</credentials>
</forms>
</authentication>