C# ASP.NET C CSS在创建会话时丢失

C# ASP.NET C CSS在创建会话时丢失,c#,asp.net,css,C#,Asp.net,Css,请帮忙!!我正在使用visual studio,在ASP.NET C.NET 4中编程: 我是一个PHP转换程序,试图使用会话创建一个简单的登录页面。鉴于我对ASP.NET一无所知,我在互联网上的某个地方使用了以下代码: Login.aspx: <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> <asp:TextBox ID="txtPassword" runat="server"><

请帮忙!!我正在使用visual studio,在ASP.NET C.NET 4中编程: 我是一个PHP转换程序,试图使用会话创建一个简单的登录页面。鉴于我对ASP.NET一无所知,我在互联网上的某个地方使用了以下代码:

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>
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");
        }
    }
每当我尝试在浏览器中运行此代码时,我的CSS文件都不会加载。这似乎链接到了global.asax文件,因为如果我在global.asax.cs代码段中注释掉上面显示的所有代码,css将正确加载

我尝试通过源代码在浏览器中打开css文件,这会导致以下错误:

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 
我还尝试过使用App_Themes使用一个主题,并在Web.config中定义它——这也会带来同样的问题。css仅在删除会话相关代码时显示


你知道是什么原因造成的吗?

问题是,当你试图加载静态资源和动态页面时,你正在检查登录名。某些文件(如全局CSS)应可用于完全匿名访问,而不应在身份验证逻辑后设置门控


您肯定是在重新发明轮子,因为由大量成员资格提供商支持的内置表单身份验证为您提供了丰富的基础,可供扩展。如果您选择继续使用此方法,我的建议是查询请求的文件,并确保他们正在请求aspx,前提是您在要求身份验证之前没有使用url重写。

我将使用。感谢提供的资源,这看起来已经是一种更好的方法!当css文件从该行传递时,则没有会话,因此解决方案是检查ifSession{},然后运行它,或者使用System.IO.Path.GetExtensionHttpContext.Current.Request.Path检查文件扩展名。当然,所有这些都不如Tim建议的表单身份验证提供程序那么好,你在使用母版页吗?如果没有,你的css在login.aspx和default.aspx上都被正确引用了吗?谢谢-直到今天早上我还不知道表单身份验证是否存在:D在使用它之后,我发现它不那么复杂,而且开箱即用的控件可以完全满足我的需要,而不需要填充任何其他内容。
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