Click here to Skip to main content
15,879,348 members
Articles / Web Development / ASP.NET
Article

Simple User Account Login

Rate me:
Please Sign up or sign in to vote.
3.44/5 (31 votes)
23 Jun 20025 min read 292.5K   8.1K   94   28
Demonstrates maintaining user account information, managing session state, Access database I/O, cookies, and some other basic features.

Sample Image - UserAccountLogin.jpg

Introduction

This is a simple demonstration of managing user account information on a server using ASP.NET. I decided that this might be a good way to get my feet wet in the whole ASP.NET environment, and the trials that I went through to get this far, might be of interest (and amusement!) to everyone else.

I am very much indebted to the numerous people on CP, who have helped answer some of my very basic questions. You all know who you are, and I thank you very much for your help.

Features

This project demonstrates the use of:

  1. Managing username, password, and configuration options in an MS Access database;
  2. Using HttpResponse.Cookies and HttpRequest.Cookies to remember the user's login name;
  3. Use of HttpSessionState to retain the user ID between web pages
  4. Some simple JavaScript to set the focus of controls on the web pages

The Journey into ASP.NET land

While embarking on this journey, I encountered a variety of "interesting" problems to solve. What follows are my notes regarding the things I learned.

HTML Controls

I wanted to use the HTML password control for passwords, since it displays "dots" for the password text. I couldn't find any web form control that did this. I'm used to the MFC edit box which has a password style. It doesn't seem that the web form edit control has this capability. I could be wrong! In any case, using HTML controls is interesting because to do anything useful with the control in the C# text, you have to convert it into a "server control", which is accomplished quite easily by right clicking on the HTML control and selecting "run as server control". Of course, it took about an hour to figure this out!

Tab order

After I set up my first login screen, I noticed the tab order was wrong. So, silly me, I went looking around the IDE for a "tab order" wizard or menu command like the one for MFC. I couldn't find it! So, instead, I moved the controls around directly in the HTML. That was fairly straight forward. Later on, I discovered the TabIndex property for control. But, since I had my controls all ordered correctly in the HTML by then, I never played with the TabIndex. Personally, I'd rather get them ordered right in the HTML.

Setting the Control ID

It's most helpful if you set the ID's of the web form control first, before creating event handlers. Otherwise, you'll end up with some non-descript name and you'll have to edit the box titled "Web Form Designer generated code" to change the names of the event handlers, etc. Not too painful, but doing it right the first time is very helpful.

Centered Labels

I had an interesting time of getting the "Welcome to my web site" text centered to the size of the panel. I couldn't find a way to center justify the web form label control, so I created an HTML label, set it as "Run As Server Control", then changed the style, etc. Is there a better way?

An annoying thing

The thing that's annoying about these HTML controls is that the method for setting text is different. For example, label text has to be set with the InnerText method, while edit box text is set with the Value method. The inconsistency makes life for beginners harder.

Databases and folder permissions

I pulled my hair out for about a day trying to figure out why I could query the database, but I couldn't update it. A friendly Code Project member replied to my message and pointed out that I needed to put the database into a separate folder that had read/write permission (among other things, so that the .LDB file could be created). I had been playing with permissions in the IIS to no avail. Doing this made everything work, except that originally, my password field was called PASSWORD, which worked fine in queries but not updates.

State, Persistence, and Server vs. Client

I must say, this whole web form stuff being stateless is quite an interesting thing to deal with. You really have to change the style in which you program, and therefore, think, about persistence, etc. Also, I'm learning that I have to segregate my thinking into server vs. client side functionality. So, for example, I was trying to figure out how to set the focus to the user name edit control using a server side method. Posing this question on CP provided me with the answer that, this is a client side thing to do. A code example was kindly provided.

As a veteran MFC/C++ programmer, even having segregated client vs. server database functionality, I find it very hard to create a mental image of what happens on the server and what happens on the client. I think this confusion is aided by the fact that ASP.NET makes this line fuzzier, with event handlers, etc.

Conclusion

I'm not going to do any code snippets here (unless requested). You can peruse the code yourself by downloading the project. It's quite straightforward. All of the code is more or less copied out of the MSDN help and the excellent .NET Framework Essentials from O'Reilly Press, and of course from numerous posts on CP. I've just glued it all together to make a simple demonstration and added some prototypes of classes that might be useful to me later on.

If anyone is interested in some controversial stuff, you can read my comments in the code about how I implemented the database interface. Don't look for encryption on the passwords or a lot of other bells and whistles. This is intended to be a demonstration program only, something of a test-bed as I get by ASP.NET, feet wet.

Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions

 
QuestionWow, I wrote an awful article Pin
Marc Clifton6-Feb-22 11:11
mvaMarc Clifton6-Feb-22 11:11 
QuestionHai getting parsing error.. <%@ Application Codebehind="Global.asax.cs" Inherits="Account.Global" %> Pin
Soorajth9-Feb-09 18:02
Soorajth9-Feb-09 18:02 
GeneralRegistration and Log In Pin
vbconcepts22-Apr-08 6:41
vbconcepts22-Apr-08 6:41 
GeneralRe: Registration and Log In Pin
mauji11-Jul-09 21:38
mauji11-Jul-09 21:38 
GeneralLogin problem Pin
kidus12-Oct-07 8:16
kidus12-Oct-07 8:16 
GeneralLogin.html Pin
kaushik.mota17-Dec-05 7:56
kaushik.mota17-Dec-05 7:56 
GeneralI'v got error plase Help!!! Pin
Member 19289097-Jul-05 5:17
Member 19289097-Jul-05 5:17 
GeneralRe: I'v got error plase Help!!! Pin
lauralucas29-Jun-06 10:18
lauralucas29-Jun-06 10:18 
GeneralRe: I'v got error plase Help!!! Pin
crop47-Sep-06 5:48
crop47-Sep-06 5:48 
GeneralGetting a Parse Error Pin
webdinamico23-Jan-05 9:15
webdinamico23-Jan-05 9:15 
GeneralRe: Getting a Parse Error Pin
kerberos_prozac30-Mar-05 13:45
kerberos_prozac30-Mar-05 13:45 
Generalbetter way Pin
idishkov14-Apr-04 22:11
idishkov14-Apr-04 22:11 
Generalaj aj aj poor coder Pin
idishkov14-Apr-04 22:04
idishkov14-Apr-04 22:04 
Generalhorrible Pin
zoelopital2-Oct-03 5:03
zoelopital2-Oct-03 5:03 
GeneralRe: horrible Pin
Marc Clifton2-Oct-03 5:20
mvaMarc Clifton2-Oct-03 5:20 
GeneralRe: horrible Pin
Oggman22-Oct-03 9:08
Oggman22-Oct-03 9:08 
GeneralRe: horrible Pin
Marc Clifton2-Oct-03 9:17
mvaMarc Clifton2-Oct-03 9:17 
GeneralRe: horrible Pin
Joel, Just Joel21-May-05 17:38
Joel, Just Joel21-May-05 17:38 
GeneralSession state Pin
jmathew5-Jul-03 11:34
jmathew5-Jul-03 11:34 
GeneralBasics, very basics Pin
jgmata23-Jun-03 6:30
jgmata23-Jun-03 6:30 
GeneralUsing information Pin
K-1914-Jan-03 11:54
K-1914-Jan-03 11:54 
GeneralTabIndex property doesn't work. Pin
Phillip H. Blanton (Old)17-Dec-02 9:33
Phillip H. Blanton (Old)17-Dec-02 9:33 
GeneralGood article Pin
LaMagra26-Aug-02 23:16
LaMagra26-Aug-02 23:16 
General"Operation must use an updateable query." Pin
Sasha Djurovic3-Jul-02 12:34
Sasha Djurovic3-Jul-02 12:34 
GeneralRe: "Operation must use an updateable query." Pin
Sasha Djurovic3-Jul-02 13:13
Sasha Djurovic3-Jul-02 13:13 
OK I solved it. I have to say that VS.NET is a great product, but who ever indexed help topics is out of their minds. Priorities are all wrong, and I didn't even find the answer there.
When you are running programs on local network, besides enabling "Web sharing" for directory you have to enable "Sharing"->"Network sharing"
DOH!!!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.