如何强制用户必须登录后才能访问网站主页? 访问网站时用户有可能通过直接在浏览器中输入URL来绕开登录过程直接访问网站主页。现在的初步方案是用一个session保存用户登录的id,然后在每个页面的page_load里判断这个Id是否null,为null时跳去登录页面。不知各位还有什么方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 basepage或ihttphanlder判断sessionpublic class BasePage : System.Web.UI.Page { public BasePage() { } protected override void OnInit(EventArgs O) { if(base.Session["UserId"] == null || base.Session["UserId"].ToString().Equals("")) { } } } if (Session["AUserName"] == null){ Response.Clear(); Response.Redirect("Login.aspx");} Global.asax 文件中进行判断void Application_BeginRequest(Object sender, EventArgs e) { if (Session["AUserName"] == null){ Response.Clear(); Response.Redirect("Login.aspx");} } void Application_BeginRequest(Object sender, EventArgs e) {//此处先得到请求页面,如果是不是login.aspx页面你就进行session判断,如果是这个页面,你直接就不用判断了! if (Session["AUserName"] == null){ Response.Clear(); Response.Redirect("Login.aspx");} }但前提你还要对login.aspx页面进行一次判断,不然会出现死循环的!! 到我的博客找一下吧 有个简单的方法 继承page类 重写方法 写了个完整的给你 //保存不用判断session页面地址 string[] _Pages ={ @"/Login.aspx"//登陆页面 }; //判断页面 bool AuthorizPage(string PageName) { bool _AuthorizPage = false; for (int i = 0; i < _Pages.Length; i++) { if (Common.Regular.IsValiable(_Pages[i], PageName.ToLower())) { _AuthorizPage = true; break; } } return _AuthorizPage; }void Application_BeginRequest(object sender, EventArgs e) { string strRawUrl = HttpContext.Current.Request.RawUrl.ToLower(); string strNewUrl; //******************************************************************************************* if (!AuthorizPage(strRawUrl)) {if (Session["AUserName"] == null){ Response.Clear(); Response.Redirect("Login.aspx");}}} Common.Regular.IsValiable这个是一个正则判断 现个字符串是不是相同,其实原来和上面几个说的一下,就是页面请求的时候就进行判断,这样维护起来方便,每个页面都写判断也可以,但不利于很好维护 搞个basepage , 判断登陆的在这里面写, 然后其他的页面继承这个类, 就不用多写判断了。。 。 <authentication mode="Forms" > <forms cookieless="UseCookies" name="./Login" loginUrl="Login.aspx" timeout="50" path="/" protection="All"/></authentication> <location path="要挡掉的路径"> <system.web> <authorization> <deny users="?" /> <allow ... /> </authorization> </system.web> </location> 恩,global.asax文件中直接到login.aspx页面 在模板里面写上一句判断Sessions是否为空,为空就转至登录页面。这样也不麻烦啊。 楼上的都给出了方法。你的每个页面判断的方法,显然不妥当,应该很快就能想到用基类的方式来处理呀,基类实现到了判断逻辑,然后所有的页面集成这个基类。还有Application的方式。 reply by 阿捷 tcp socket能发不能收,这是为什么? 刚学C#,菜鸟请教! VS2005下水晶报表中如何读取数据中存储的图片显示出来 在线求助!UltraWinGrid的Row插入问题 想改行了。由vb.net改为C#.net,代码及思想是差不多的。不过有点小问题我不明白请大家指点。 那有可以免费下载的vs2005 一个textbox的简单问题?? 菜问题:各种各样的错误。1.无法找到资源 2.在建立与服务器的连接时出错 3.配置错误 C# socket断线重连等异常情况处理 请教:编译FreeTextBox出现的问题 serialPort显示数据问题 看下面链接- -
public class BasePage : System.Web.UI.Page
{
public BasePage()
{ }
protected override void OnInit(EventArgs O)
{
if(base.Session["UserId"] == null || base.Session["UserId"].ToString().Equals(""))
{
}
}
}
if (Session["AUserName"] == null)
{
Response.Clear();
Response.Redirect("Login.aspx");
}
{
if (Session["AUserName"] == null)
{
Response.Clear();
Response.Redirect("Login.aspx");
} }
void Application_BeginRequest(Object sender, EventArgs e)
{
//此处先得到请求页面,如果是不是login.aspx页面你就进行session判断,如果是这个页面,你直接就不用判断了!
if (Session["AUserName"] == null)
{
Response.Clear();
Response.Redirect("Login.aspx");
} }但前提你还要对login.aspx页面进行一次判断,不然会出现死循环的!!
string[] _Pages ={
@"/Login.aspx"//登陆页面
};
//判断页面
bool AuthorizPage(string PageName)
{
bool _AuthorizPage = false;
for (int i = 0; i < _Pages.Length; i++)
{
if (Common.Regular.IsValiable(_Pages[i], PageName.ToLower()))
{
_AuthorizPage = true;
break;
}
}
return _AuthorizPage;
}
void Application_BeginRequest(object sender, EventArgs e)
{
string strRawUrl = HttpContext.Current.Request.RawUrl.ToLower();
string strNewUrl;
//*******************************************************************************************
if (!AuthorizPage(strRawUrl))
{
if (Session["AUserName"] == null)
{
Response.Clear();
Response.Redirect("Login.aspx");
}}
}
<forms cookieless="UseCookies" name="./Login" loginUrl="Login.aspx" timeout="50" path="/" protection="All"/>
</authentication> <location path="要挡掉的路径">
<system.web>
<authorization>
<deny users="?" />
<allow ... />
</authorization>
</system.web>
</location>
你的每个页面判断的方法,显然不妥当,应该很快就能想到用基类的方式来处理呀,基类实现到了判断逻辑,然后所有的页面集成这个基类。还有Application的方式。