ASP.NET - login issue

Asked By samaira samaira
19-Oct-11 09:34 AM
In my log-In page I have given a lable which is visible only when user logs in.
but if i compile again without logging out the previous solution it will take me to login page with the text displayed in login page which is supposed to be displayed only when user logs In.
I have used  Session.Clear();
        Session.Abandon(); in button cick event.
i.e, eg: In gmail if user doesn't logout and closes the window ,n then later types the gmail url , it should take it to logged in page.
  R B replied to samaira samaira
19-Oct-11 09:38 AM

Hello,

 

Try following approach,

 

If You have used master page and Form Authenication then write following code in Master Page Load event.

 

 

      protected void Page_Load(object sender, EventArgs e)

      {

        if (HttpContext.Current.User.Identity.Name == null)

        {

          FormsAuthentication.SignOut();

          Response.Redirect("~/Login.aspx");

        }

      }              

      

 

 

Hope this is helpful !

 

Thanks

 

 

 

 

  Reena Jain replied to samaira samaira
19-Oct-11 09:40 AM
hi,

If you want to expire the session even if a user Closes the Web Page  instead of Clicking the LogOut button, You have to Write the Codes in Application_End / Session_End of the Global.asax File.

In the LogOut button, use this code:


FormsAuthentication.SignOut();
// This redirects them to the login page
Reponse.Clear();
Repsonse.Redirect(login.aspx);


and in the protected pages , Put this in the page_load

Response.Cache.SetExpires(DateTime.Now);


Beacuse when user click the "Back" button in the browser even though user signed out,  the content of the secure page which previously visited can still be displayed. so implementing the above code in the page_load when prevent the back button.

or
Try adding <%@ OutputCache location="none" %> directive on top of your secured pages.

or

Disabling cache can also be done by adding following line in Meta section of page

<METAHTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">

or

add following code in the page load of the page for which we do not want to cache the page in history.

Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d);
Response.Expires =-1500;
Response.CacheControl = "no-cache";
Session.Abandon();

hope this will help you
  Santhosh N replied to samaira samaira
19-Oct-11 09:42 AM
May be the browser instance has that somehow and compiling means is it rebuilding your solution and opening in new browser window or using same window and just compiling the solution.

Upon rebuilding the solution and if the site is under IIS, after building it should go to login page without session variables displayed in the page
  dipa ahuja replied to samaira samaira
19-Oct-11 11:24 AM
Better to use the in build login controls. It will provide you the same functionality as we get in most sites. and you can take LoginView control to handle this issue that is which controls you want to display when login and which when logout. for ex:

<asp:LoginView ID="LoginView3" runat="server">
<LoggedInTemplate>
 <asp:LinkButton ID="LinkButton3" runat="server" Text="My Profile" PostBackUrl="#" /><br />
    <asp:LoginStatus LoginText="Welcome Guest!!" LogoutText="Sign Out" ID="LoginStatus" runat="server" />          
</LoggedInTemplate>      
<AnonymousTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" Text="Login" PostBackUrl="Login" /><br />
</AnonymousTemplate>
</asp:LoginView>
  samaira samaira replied to Santhosh N
19-Oct-11 11:44 PM
It's rebuilding my solution and opening in new browser window & if am not signed out before compiling , the solution opened in new window has got the session variables displayed which is supposed to be displayed in  logged In page.
I want it to take me to logged In page if am not logged out and accessing the url again. Eg :gmail
  samaira samaira replied to Reena Jain
19-Oct-11 11:51 PM
am not using form authentication because i had some issued using it, am using login view n login controls though.
I have manually dislayed "welcome username" n logout in master page, which is invisible in login page n visible in loggedIn page! below is the code used in master.cs page:

protected void Page_Load(object sender, EventArgs e)
      {
        string sesn = Session["EmpName"].ToString();
        
       
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetAllowResponseInBrowserHistory(false);
        Session.Clear();
        Session.Abandon();
      if (!IsPostBack)
      {
       /* datap.SelectCommand = cmd;
      .
      .
      .
      
        cmd.Connection = conn;
        conn.Open();
        DataSet dataset = new DataSet();
        datap.Fill(dataset); */
       
        
        if (Session["Empname"] != null)
        {
          string ssn = Session["EmpName"].ToString();
          lblwelcome.Text = "Welcome " + ssn + " !";
          LinkButton1.Text =" Log out";
        }
        else
        {
          LinkButton1.Text = "Log In";
          lblwelcome.Visible = false;
          LinkButton1.Visible = false;
       
         
        }
      }
    }
      protected void btnlogout_Click(object sender, EventArgs e)
    {
      if (Session["EmpName"] != null)
      {
       
     
        Response.Redirect("~/Forms/Login.aspx");
        
      }
      
      
    }






This is the code used in logged In page
 protected void Page_Load(object sender, EventArgs e)
      {
 Response.Buffer = true;
        Response.ExpiresAbsolute = DateTime.Now.AddHours(-1);
        Response.Expires = 0;
        Response.CacheControl = "no-cache";
        //Page.RegisterClientScriptBlock("<script>if(history.length>=0)history.go(+1);</script>");
      
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetAllowResponseInBrowserHistory(false);
Create New Account
help
alot var Hi What are Master Pages in ASP.NET? or What is a Master Page? ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page. What are the 2 important parts of a master page? The following are the 2 important parts of a master page 1. The Master Page itself 2. One or more Content Pages Can Master Pages be nested? Yes, Master Pages
Page Directive? What is the purpose of page directive in aspx page? Directive Syntax Directives are instructions used to specify settings (related to how a page should render and processed) used by the page and user control compilers when they process ASP.NET Web Forms page (.aspx) and user control (.ascx) files. These are the essential part of every ASP.NET Page or Control. Directives can be located anywhere in an .aspx or .ascx file, but the values, same as any HTML tag) that are specific to that directive. Special Note: The @ Page directive can be used only in .aspx files, and the @ Control directive can be used
this.tabControl1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } private void tab1Button1_Click(object sender, System.EventArgs e) { / / Inserts the code that should run when the button is clicked. } void tabControl1_Selected(object e.TabPage)) { _cache.Remove(e.TabPage); } _cache.Add(e.TabPage); } } private void button1_Click(object sender, EventArgs e) { TabPage page = new TabPage("TabPage" + pageNumber.ToString()); AddControlsToTabPage(page); this.tabControl1.TabPages.Add(page); pageNumber++; this.tabControl1.SelectedTab = page; } private void AddControlsToTabPage(TabPage page) { page.Controls.Add(this.tab1CheckBox1); page.Controls.Add(this.tabCheckBox2); page.Controls.Add(this.tabCheckBox3); page
Utilities; using System.Text.RegularExpressions; using System.Net; public partial class Production_OA_Config : System.Web.UI.Page { #region Public Variables & Objects ExceptionHandler objHandlers; OAEntity _oaEntity = new OAEntity(); List<object> obj = new List string Hieght_Y = ""; public string Time = ""; / / - -- -- -- public int _Part = 0; #endregion protected void Page_Load(object sender, EventArgs e) { try { if (Session["UserId"] ! = null) { User_Id = Convert.ToInt32(Session["UserId"].ToString()); if (!IsPostBack) { / / LoadInitialSetting ScriptManager.RegisterStartupScript(this, this.GetType(), "popup message", "setconformation();setScrollerWidth();slidStart();", true); if (CadPartListpopUp ! = null) { / / ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "cadpop", "document.getElementById('" + CadPartListpopUp.ClientID + "').style.display = '';ShowBackShadow('dvbackShadow', true);", true); } } catch (ExceptionHandler ex) { ErrorHandler.SetMessage(ex.MessageDetails(), spnmessage); / / ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "popup message", "document.getElementById('" + ErrorPop.ClientID + "').style.display = '';", true); ScriptManager RegisterClientScriptBlock(this, this.GetType(), "popup message", "Message_MoveTO_popup('" + spnmessage.ClientID + "', 'spnmessage');JqueryPopupShow();", true); } catch (Exception ex) { objHandlers = new ExceptionHandler(ex, true); ErrorHandler.SetMessage(objHandlers.MessageDetails(), spnmessage); / / ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "popup message", "document.getElementById('" + ErrorPop.ClientID + "').style.display = '';", true); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "popup message", "Message_MoveTO_popup('" + spnmessage.ClientID + "', 'spnmessage');JqueryPopupShow();", true); } } private void BindActor() { / / ddlCustomer name = "sender"> < / param> / / / <param name = "e"> < / param> protected void imgbtnAddCadPro_OnClick(object sender, ImageClickEventArgs e) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "cadpop", "document.getElementById('" + CadProdListpopUp.ClientID + "').style.display = '';ShowBackShadow('dvbackShadow', true);", true); Chk_Product_Amada_CadCAM_Service