Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8309663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:11:30+00:00 2026-06-08T19:11:30+00:00

Please help!! I’m using visual studio, programming in ASP.NET C# (.Net 4): I’m a

  • 0

Please help!! I’m using visual studio, programming in ASP.NET C# (.Net 4):
I’m a PHP convert, trying to create a simple login page using a session. Seeing as I’m completely new at ASP.NET I used this code from somewhere on the internet:

Login.aspx:

<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
<asp:Button ID="submit" runat="server" Text="Submit" onclick="submit_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Login.aspx.cs:

protected void send_Click(object sender, EventArgs e)
        {
        if (txtUserName.Text == "admin" && txtPassword.Text == "admin")
        {
            Session["Authenticate"] = "Yes";
            Response.Redirect("Default.aspx");
        }
        else
            Label1.Text = "Login failed";
        }

Global.asax.cs

void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        Session["Authenticate"] = "";
        CheckLogin();
    }
void Application_OnPostRequestHandlerExecute()
    {
        CheckLogin();
    }

    void CheckLogin()
    {
        string Url = Request.RawUrl;
        int count = Url.Length - 10;
        string TestUrl = Url.Substring(count);
        string SessionData = Session["Authenticate"].ToString();
        if (SessionData == "" && TestUrl != "Login.aspx")
        {
            Response.Redirect("~/Login.aspx");
        }
    }

Whenever I try to run this code in the browser, my CSS file doesn’t load. This appears to be linked to the global.asax file, because the css load correctly if I comment out all of the code shown above in my global.asax.cs snippet.

I have tried opening the css file in the browser via the source code, which gives the following error:

Session state is not available in this context.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Session state is not available in this context.

Source Error: 


Line 45:             int count = Url.Length - 10;
Line 46:             string TestUrl = Url.Substring(count);
Line 47:             string SessionData = Session["Authenticate"].ToString();
Line 48:             if (SessionData == "" && TestUrl != "Login.aspx")
Line 49:             {

Source File: D:\MyTestWebsite\Global.asax.cs    Line: 47 

I have also tried using a Theme using App_Themes and defining it in Web.config – gives the same problem. The css only shows when the session-related code is removed.

Any idea what’s causing this???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:11:31+00:00Added an answer on June 8, 2026 at 7:11 pm

    The issue is that you’re checking login when attempting to load your static resources as well as dynamic pages. Certain files (such as global CSS) should be available for full anonymous access and not gated behind authentication logic.

    You are definitely reinventing the wheel as the built in Forms Authentication supported by a large library of membership providers provide a rich base for you to expand upon. If you do choose to go forward with this method, my suggestion would be to query the requested file and ensure that they are requesting an aspx (assuming you are not using url rewriting) before requiring authentication.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

please help i have the following php code for my login session but i
please help me with asp.net MVC 2 application. I have class: public class Account
please help me I'm working on an asp.net application have a main site and
Please help me ! I have a problem in Crystal Report at visual studio::
Please help me somebody to convert unicodestring into string This is how i am
Please help me to improve the following script: http://jsfiddle.net/n9BkM/8/ I need the following fucntionality:
Please help me, I'm trying to find some documentation about pre-installed applications at Android.
Please help me look into my simple test case: import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.RootPanel; import
Please help me in printing the complete object using console.log method. If I am
Please Help me to do this first i have this div <div class=loader alt=stat.php>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.