none
I need two username and password in a single login page

    질문

  • Hi guys, i have created a login page for my project. I have done it with a single username and password. Now i want to add another userid and password so that there will be two login id's and password. Kindly help me.

    Login.aspx.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Collections.Generic
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.Linq;

    public partial class Login : System.Web.UI.Page
    {
        Validation val = new Validation();
        string res = "";
        protected void ctlLogin_Authenticate(object sender, AuthenticateEventArgs e)
        {
           
            string[] UserName = { "Demoid01", "Demoid02" };
            string[] password = { "demoid@001", "demoid@002" };
            string[] Username1 = UserName.Split(",");
            if (ctlLogin.UserName.Equals(UserName) && ctlLogin.Password.Equals(password))
                e.Authenticated = true;
            else
                e.Authenticated = false;

        }

       
        protected void ctlLogin_LoggedIn(object sender, EventArgs e)
        {
           
            FormsAuthentication.RedirectFromLoginPage(ctlLogin.UserName, false);

        }

    }

    This is my aspx.cs file. In this file am Doing everything.

    2012년 6월 12일 화요일 오후 1:54

모든 응답

    1. This is a situation in which you really should have a database, rather than hard coding the login information into the program itself.  You can have a 'User' table with a column for username and password, and then run a query to look for an item where the username and password equal the user supplied data.
    2. If it's absolutely impossible to use a database, then I'd create a Dictionary that holds onto the login information rather than using arrays like you have there.  Have the username as the key, and the password as the value.  You can check if the username entered is in the dictionary, and if it is then compare the user supplied password with the password that corresponds to that key in the dictionary.
    3. It's best to not store the password in plaintest, either in code or in a database.  You should store the hash of that password so that even if some malicious user is able to get ahold of the hash, they still can't login to the system or even determine what the password was.  The idea is simple.  Choose a hashing algorithm (.NET has several implementations in it's library, so you don't even need to code them yourself).  Hash your passwords using that algorithm, store the hashes, and don't store the actual passwords anywhere.  When a user sends a username and password, hash the password right away.  If the two hashes match, they are the same password.
    2012년 6월 12일 화요일 오후 2:13
  • @servy42  I need to hardcore the username and password in my code itself. that is the requirement. Dictionary method is useful but i never used it. can you tell me how to do it??
    2012년 6월 12일 화요일 오후 3:49
  • Better create a new class like LoggesUsers, and then create a list<T> (T will be this class) and add each user`s data into list.

    Example:

    class LoggedUser
    {
        public string Username {get; set:}
        public string Password {get; set;}
    }
    
    inside your class on a load time instantiate new list:
    List<LoggedUser> users;
    void LoadingTiem()
    {
        users = new List<LoggedUser>();
    }
    
    //in that ctlLogin_Authenticate method do after all ok:
    LoggedUser user = new LoggedUser();
    user.UserName = "some name";
    user.Password = "some passowrd";
    users.Add(user);
    //do the same for other users!


    Mitja

    2012년 6월 12일 화요일 오후 4:14
  • @servy42  I need to hardcore the username and password in my code itself. that is the requirement. Dictionary method is useful but i never used it. can you tell me how to do it??

    Create a new dictionary with a string key and value.  Use the 'Add' method to add the username/password combinations.  Use the 'ContainsKey' method to determine if an item already exists.  Use the indexer to get a value for a particular key (i.e. string someValue = myDictionary["someKey"]; ).

    You can look at the MSDN page for Dictionary along with doing some web searches for more in-depth tutorials on how to use a Dictionary.

    Once you start working on the project you should feel free to post an updated code snippet if you are having problems or just would like me to look over your code.

    2012년 6월 12일 화요일 오후 4:18
  • Hi ,

    Please use the ASP.NET forums for ASP.NET related questions: http://forums.asp.net/


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    2012년 6월 15일 금요일 오전 8:59
  • Hi ,

    Please use the ASP.NET forums for ASP.NET related questions: http://forums.asp.net/


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    The code might happen to be executing in an ASP environment, but it's not dependent on any ASP related features.  He's not asking how to use ASP to ask the user for a username/pass, merely how to authenticate a username/password pair, as strings, in memory, which is entirely a general C# problem.  That makes C# general, in my mind, a perfectly acceptable place to ask this question.
    2012년 6월 15일 금요일 오후 1:37