• 0

C# and ASP Login Form


Question

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

  • 0

no worries.

the example on that site is a very basic example.. but you can make it work with windows authentication and also store user permissions / levels in a custom database if required.

i have not used it myself for about 3 years, but can try and answer questions if you have them :)

so, firstly.. what login are you using? forms or windows authentication?

Link to comment
Share on other sites

  • 0

forms you get a logon interface where you type a username and password..

windows uses the logon you used to log into windows

the former is generally used on public websites, and the later on intranet sites within an organisation, although i have seen exceptions :)

so to rephrase, what how do you want your login functionality to work, how do you want to manage it?

Link to comment
Share on other sites

  • 0

Ah i understand that now, cheers,

I want users to be able to register on the site, this information to be saved into an Access database and them to then be able to log in using this information.

I'm guessing ill need to use forms for this?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.