如果用户未登录,则转到登录页面. [英] If user is not logged in then move to login page.

查看:211
本文介绍了如果用户未登录,则转到登录页面.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在房地产网站上工作.有页面-发布物业,发布您的要求.我想让未注册的用户单击发布属性"或发布您的需求"页面,然后重定向到登录页面,否则转到被单击的页面.


请给我一个建议怎么做?

Hi,

I am working on real estate website.There are pages-Post Property, post your requirement. i wanna if an unregistered user click on Post Property or Post your Requirement page then redirect to login page otherwise go to clicked page.


Please give me a suggestion how to do?

推荐答案

if(session["userid"]=="")
{
  response.redirect("Loginpage.aspx");
}



谢谢



Thanks


这里有两个链接可让您充分了解与之相关的这些观念:

了解ASP.NET角色和成员资格-入门教程 [ ^ ]
了解并实现ASP.NET自定义表单身份验证 [ ^ ]

如果您需要一种快速的方法,那么这里有一个做同样的事情的项目.您可以在此处找到一些易于使用的代码:
YaBlogEngine-用ASP.NET/编写的微型博客引擎C# [ ^ ]

像这样的代码:

Here are two links to get the full understadning of these oncepts related to it:

Understanding ASP.NET Roles and Membership - A Beginner''s Tutorial[^]
Understanding and Implementing ASP.NET Custom Forms Authentication[^]

If you need a quick way of doing it then here is a project doing the same stuff. you can find some quick-to-use code here:
YaBlogEngine - A Tiny Blog Engine written in ASP.NET/C#[^]

code like:

protected void Page_Load(object sender, EventArgs e)
      {
          if (Session["UserId"] == null)
          {
              Response.Redirect("Login.aspx");
          }
      }


if (!session["UserID"]==null && !session["UserName"]==null) 
{
   labelID.text= (string) Session["UserID"];
   labelUserName.text= (string) Session["UserName"];
}
else
{
   Response.Redirect("~/login.aspx");
}



使用此方法,您要检查会话中是否存在用户ID和用户名,然后在标签上显示信息(如果不存在),将其重定向到login.aspx.


在注销代码中,不要重写会话变量的值,而要删除它们,请使用



using this, you are checking if user id and the user name are present in session if so then displaying the info on labels, if any of them doesnt exist, redirecting them to login.aspx.


in your log out code, dont rewrite the value of the sessional variables, instead drop them, use

Session.Clear();


清除所有会话值.


to clear all the sessional values.


这篇关于如果用户未登录,则转到登录页面.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆