Adding an encrypted password and the user name in web.config

I came with this snippet for one of my project to store a username and his password encrypted in web config.
Just create a web form with two textboxes Username and UserPassword, and a button btAdd.

Add this code in the code behind of the page.


Imports System.Text
Imports System.Web.Security
Imports System.Xml

Private Sub btnAdd_Click(ByVal sender As Object_
    ByVal e As System.EventArgsHandles btnAdd.Click

  Dim MyPassword As String = UserPassword.Value
  Dim MyName As String = Username.Text

  If MyName <> "" And MyPassword <> "" Then

    Dim doc As New XmlDocument()

    doc.Load(Server.MapPath("../Web.config"))

    Dim newElement As XmlElement = doc.CreateElement("user")

    Dim Nameattribute As XmlAttribute = doc.CreateAttribute("name")
    Nameattribute.Value = MyName

    Dim Passwordattribute As XmlAttribute = doc.CreateAttribute("password")
    Passwordattribute.Value = FormsAuthentication.HashPasswordForStoringInConfigFile_
        strPlainText_
        "sha1")

    newElement.Attributes.Append(Nameattribute)
    newElement.Attributes.Append(Passwordattribute)

    doc.Save(Server.MapPath("../Web.config"))

    txtPassword.Value = ""
    txtUsername.Text = ""
  End If

End Sub

1 Comment

Comments have been disabled for this content.