Hello:
I'm receiving the message, "Object reference not set to an instance of an object" when I run my program. I'm attempting to create passwords in the Web.Config file dynamically at runtime. My error occurs when it hits this line, "credentials.AppendChild(newElement)". Any help would be appreciated.
My code follows:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnLogin.Click
Dim strPlainText As String = txtPassword.Text
Dim strUsername As String = txtUserName.Text
If strPlainText <> "" And strUsername <> "" Then
Dim strHash As String = _
FormsAuthentication.HashPasswordForStoringInConfigFile( _
strPlainText, _
"sha1")
strPlainText = ""
'**********************************************
'Open config file and build new password digest
'**********************************************
Dim doc As New XmlDocument
doc.Load(Server.MapPath("..\Faculty\Web.config"))
Dim newElement As XmlElement = doc.CreateElement("user")
Dim attribName As XmlAttribute = doc.CreateAttribute("name")
attribName.Value = strUsername
Dim attribPassword As XmlAttribute = doc.CreateAttribute("password")
attribPassword.Value = strHash
newElement.Attributes.Append(attribName)
newElement.Attributes.Append(attribPassword)
Dim credentials As XmlElement = doc.GetElementsByTagName("credentials").Item(0)
credentials.AppendChild(newElement)
doc.Save(Server.MapPath("../Faculty/Web.config"))
lblResults.Text = "User saved. Username:" & _
strUsername & " Password:" & strHash
lblResults.Visible = True
txtPassword.Text = ""
txtUserName.Text = ""
End If
If FormsAuthentication.Authenticate(txtUserName.Text, txtPassword.Text) Then
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, chkAutomatic.Checked)
Else
lblError.Text = "Incorrect user name or password. Please try again."
End If
End Sub
I'm receiving the message, "Object reference not set to an instance of an object" when I run my program. I'm attempting to create passwords in the Web.Config file dynamically at runtime. My error occurs when it hits this line, "credentials.AppendChild(newElement)". Any help would be appreciated.
My code follows:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnLogin.Click
Dim strPlainText As String = txtPassword.Text
Dim strUsername As String = txtUserName.Text
If strPlainText <> "" And strUsername <> "" Then
Dim strHash As String = _
FormsAuthentication.HashPasswordForStoringInConfigFile( _
strPlainText, _
"sha1")
strPlainText = ""
'**********************************************
'Open config file and build new password digest
'**********************************************
Dim doc As New XmlDocument
doc.Load(Server.MapPath("..\Faculty\Web.config"))
Dim newElement As XmlElement = doc.CreateElement("user")
Dim attribName As XmlAttribute = doc.CreateAttribute("name")
attribName.Value = strUsername
Dim attribPassword As XmlAttribute = doc.CreateAttribute("password")
attribPassword.Value = strHash
newElement.Attributes.Append(attribName)
newElement.Attributes.Append(attribPassword)
Dim credentials As XmlElement = doc.GetElementsByTagName("credentials").Item(0)
credentials.AppendChild(newElement)
doc.Save(Server.MapPath("../Faculty/Web.config"))
lblResults.Text = "User saved. Username:" & _
strUsername & " Password:" & strHash
lblResults.Visible = True
txtPassword.Text = ""
txtUserName.Text = ""
End If
If FormsAuthentication.Authenticate(txtUserName.Text, txtPassword.Text) Then
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, chkAutomatic.Checked)
Else
lblError.Text = "Incorrect user name or password. Please try again."
End If
End Sub