Save user account to an XML file : FormsAuthentication « Login Security « ASP.Net






Save user account to an XML file


<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.Security" %>
<html>
<head>
<title>Registration Page</title>
<script runat="server">
   Sub Register_Click(Sender As Object, e As EventArgs)
      If Page.IsValid Then
         Dim LoginDS as New DataSet()
         LoginDS.ReadXml(Server.MapPath("Users.xml"))
         If LoginDS.Tables(0).Select("Email='" & Email.text & "'").Length = 0 Then
            Dim NewUser As DataRow
            NewUser = LoginDS.Tables(0).NewRow()
            NewUser("Email") = Email.Text
            NewUser("Password") = FormsAuthentication.HashPasswordForStoringInConfigFile(Password.Text, "SHA1")
            LoginDS.Tables(0).Rows.Add(NewUser)
            LoginDS.WriteXml(Server.MapPath("Users.xml"))
            Response.Redirect(Request.QueryString("Page"))
         Else
            Message.Text = "User with email: <i>" & Email.Text & "</i> already exists. Please choose another email address."
         End If
      End If
   End Sub
</script>
</head>
<body>
   <form runat="server">
      <table border="0" cellspacing="10">
         <tr>
            <td>Email: </td>
            <td><asp:textbox id="Email" runat="server"/></td>
         </tr>
         <tr>
            <td>Desired Password: </td>
            <td><asp:textbox id="Password" textmode="Password" runat="server"/></td>
         </tr>
         <tr>
            <td>Confirm Password: </td>
            <td><asp:textbox id="PasswordConfirm" textmode="Password" runat="server"/></td>
         </tr>
         <tr>
            <td><asp:button text="Submit" onclick="Register_Click" runat="server"/></td>
            <td><input type="reset" value="Cancel" runat="server"/></td>
         </tr>
      </table>
      <asp:comparevalidator id="comparePasswords" 
         controltovalidate="Password" 
         controltocompare="PasswordConfirm"
         display="dynamic"
         text="Passwords must match!"
         operator="Equal"
         runat="server"/>
      <asp:requiredfieldvalidator id="requireEmail"
         controltovalidate="Email" 
         display="dynamic"
         text="Email address required!"
         runat="server"/>
      <asp:requiredfieldvalidator id="requirePassword"
         controltovalidate="Password" 
         display="dynamic"
         text="Password required!"
         runat="server"/>
      <asp:label id="Message" runat="server"/>
   </form>
</body>
</html>

file: Users.xml

<?xml version="1.0" standalone="yes"?>
<Users>
  <User>
    <Email>a@asp.com</Email>
    <Password>816010E041FA485C6E2383C649343D3A0CAD4D25</Password>
  </User>
</Users>

 








Related examples in the same category

1.FormsAuthentication.Authenticate (C#)
2.FormsAuthentication.RedirectFromLoginPage (VB)
3.Checking credentials in SQL Server (VB)
4.Checking credentials in SQL Server (C#)
5.assigning a name to the user accessing next pages
6.FormsAuthentication.SignOut()
7.Hash password