I'm not completely sure if this is the correct area to post so sorry if it isn't!
I'm making a website and need some help with a a login form, firstly I want to link the login to a database and take the details from this. I also need help with setting up member access as i'm unsure how to ensure content on the members page can only be accessed if you are logged in.
Any help would be greatly appreciated, cheers :)
I've included some of the code I have so far below
Question
woolm
Hi Everyone :)
I'm not completely sure if this is the correct area to post so sorry if it isn't!
I'm making a website and need some help with a a login form, firstly I want to link the login to a database and take the details from this. I also need help with setting up member access as i'm unsure how to ensure content on the members page can only be accessed if you are logged in.
Any help would be greatly appreciated, cheers :)
I've included some of the code I have so far below
Default.aspx Form Snippet <asp:label id="output" runat="server" /> <% if(Session["authorised"] == null && Session["user"] == null) { %> <form id="Login" name="login" runat="server" method="post" action="Default.aspx"> <label for="userName"> Username:</label> <input type="text" name="userName" id="userName" /> <label for="passWord">Password:</label> <input type="password" name="passWord" id="passWord" /> <input type="submit" name="submit" value="Login" class="sub" /> </form> <% } else { %> <% } %>Default.aspx.cs using System; public partial class _Builder : System.Web.UI.Page { private bool loggedIn = false; private string userName = ""; private string passWord = ""; private static string[] userNames = new string[] { "test"}; private static string[] passWords = new string[] { "test"}; public void Page_Load(object sender, System.EventArgs e) { if (IsPostBack) { userName = Request.Form["userName"].ToString().Trim(); passWord = Request.Form["passWord"].ToString().Trim(); for (int i = 0; i < userNames.Length; i++) { if (userName == userNames[i] && passWord == passWords[i]) { Session["user"] = userName; Session["authorised"] = true; loggedIn = true; Response.Redirect("SecretData.aspx"); break; } } if (loggedIn == false) { output.Text += "You entered an incorrect username and/or password."; } } } }SecretData.aspx.cs - (members page) using System; public partial class _Builder : System.Web.UI.Page { public void Page_Load(object sender, System.EventArgs e) { if (Session["authorised"] != null && Session["user"] != null) { output.Text = "Welcome to My Zeon"; } else { output.Text = "<p>You have been logged out, please login again</p>"; } } }Link to comment
Share on other sites
8 answers to this question
Recommended Posts