Thursday, 10 January 2013

How to edit form authentication Credentials in web.conig

Add Namespaces:


using System.Web.Configuration;
using System.Configuration;

web.config code:

<system.web>
<compilation debug="true">
</compilation>
<authentication mode="Forms">
<forms loginUrl="Default.aspx" defaultUrl="http://www.google.com/">
<credentials passwordFormat="Clear">
<user name="raghu" password="dayakar" />
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
</system.web>


Design Code:

<body>
<form id="form1" runat="server">
<div>
<br />
Enter Your Old Name:   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
Enter Your New Name:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
Enter Your  New Pasword: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<br />
</div>
</form>
</body>

Code Behind Code:

protected void Button1_Click(object sender, EventArgs e)
        {
            Configuration webconfig = WebConfigurationManager.OpenWebConfiguration("~");

SystemWebSectionGroup sysweb = (SystemWebSectionGroup)webconfig.GetSectionGroup("system.web");

AuthenticationSection authSection = sysweb.Authentication;

FormsAuthenticationUserCollection users = authSection.Forms.Credentials.Users;
FormsAuthenticationUser user = users[TextBox1.Text]; 
user.Name = TextBox2.Text.ToString();
user.Password = TextBox3.Text.ToString();


webconfig.Save();
Response.Write("<script> alert(' sucessully updated') </script>");
        }


No comments:

Post a Comment