pass values or data from one page to another when you use Login control in ASP.NET 2.0

后端 未结 4 1554
盖世英雄少女心
盖世英雄少女心 2021-01-26 01:12

I am using Login Control available in ASP.NET 2.0 in the login page. Once the user is authenticated successfully against database, I am redirecting the user to home.aspx. Here,

相关标签:
4条回答
  • 2021-01-26 01:56

    yes as Andrew said, session is the primary place to store sensitive data.

    but why a user's name is sensitive ? You can save it in cookie and print it in your home.aspx whenever user comes in.

    EDIT : You can use cookies in ASP.NET like that :

    // Setting cookie : 
    Response.Cookies["UserName"].Value = "Erhan";
    Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(7); // Persists 1 week
    
    // Getting cookie : 
    string username = string.Empty;
    if(Request.Cookies["UserName"] != null)
    {
        username = Server.HtmlEncode(Request.Cookies["UserName"].Value);
    }
    

    NOTE : Cookies stored at client's machine. so you should not use them to store sensitive data.

    0 讨论(0)
  • 2021-01-26 01:57

    If you are using standard asp.net authentication then you should be able to access the users name through the User property of the page object.

    User.Identity.Name

    As Andrew suggested, Session is a common place to place a user name, though I avoid using session at all if possible.

    You could set a cookie with the users name.

    You could also set the DestinationPageUrl property of the login control to include the username in the query string. Though that feels/looks pretty lame.

    0 讨论(0)
  • 2021-01-26 02:08

    One good place for that kind of data would be in session. Try something like this on the first page:

    this.Session["UserName"] = userName;
    

    and then subsequent pages in that session for that user could access this.Session["UserName"].

    The best thing to do though is to create a static class to manage Session for you like so:

    using System;
    using System.Web;
    
    static class SessionManager
    {
        public static String UserName
        {
            get
            {
                return HttpContext.Current.Session["UserName"].ToString();
            }
            set
            {
                HttpContext.Current.Session["UserName"] = value;
            }
        }
    
            // add other properties as needed
    }
    

    Then your application can access session state like this:

    SessionManager.UserName
    

    This will give you maximum flexibility and scalability moving forward.

    0 讨论(0)
  • 2021-01-26 02:13

    As ScottS said, if you're using the standard login controls and a membership provider this information is already available to you in User.Identity.Name.

    The only reason I'm posting an answer is to mention the LoginName control, which you can drop on a page/master page and have this done automatically for you:

    <asp:LoginName id="LoginName1" runat="server" FormatString ="Welcome, {0}" />
    

    This will render out "Welcome, Zhaph" when the user is logged in, or nothing if they are not.

    You can also combine this quite nicely with the LoginView and LoginStatus controls:

    <asp:LoginView ID="RegisterLink" runat="server">
      <AnonymousTemplate>
        <div class="titleRegistration">
          <a href="/Users/Register.aspx">Register</a> or 
        </div>
      </AnonymousTemplate>
      <LoggedInTemplate>
        <div class="titleRegistration">
          Welcome back
            <asp:LoginName ID="LoginName1" runat="server" /> -
        </div>
      </LoggedInTemplate>
    </asp:LoginView>
    <asp:LoginStatus ID="lsGeneral" runat="server"
      LogoutPageUrl="/Users/Logout.aspx" />
    

    This combination of controls will do the following:

    1. If the user isn't logged in display: Register or Login
    2. If the user is logged in display: Welcome back Zhaph - Logout

    The Login links is populated by the settings in web.config, and generated by the LoginStatus control.

    0 讨论(0)
提交回复
热议问题