如何才允许我的asp.net网站页面仅从登录页面打开? [英] How can I allow pages of my asp.net web site open from a login page only?

查看:130
本文介绍了如何才允许我的asp.net网站页面仅从登录页面打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to open all pages in asp.net website should open if user first open login page.
Do not want open any page from direct url, and if user enter any url then redirect to login page.
Currently I am Using a Session variable and I True it before Redirect page and On page load of 
other page I Check whether it is True Or false and then False at the end of page load.

推荐答案

ASP .NET提供了在页面加载(即渲染)之前执行某些操作的机制。

Page .PreInit [ ^ ]方法被调用在页面初始化之前。



该事件使您可以设置页面生命周期后期使用的值。您可以为所请求的页面动态设置母版页或主题,并创建动态控件

(每个该死的页面都有此方法)



所以你可以做的是,检查每个页面上 PreInit 方法上的会话 (除了你的登录),就像这样,

ASP .NET provides mechanism for doing something before the page is loaded i.e rendered.
Page.PreInit[^] methods is invoked before the page is initialized.

The event enables you to set values that are used later in the page life cycle. You can dynamically set a master page or a theme for the requested page, and create dynamic controls
(Each and every damn page has this method)

So what you can do is, check the Session on this PreInit method on every page (Except your Login), like this way,
onPreInit(... ...)
{
    if(Session["UserName"] == null)
    Response.Redirect("Login.aspx");
}



-KR


-KR


基本上,如果此页面是,您无法限制对您网站上任何页面的访问权限完全可以访问。请注意,您不要求只有经过身份验证的用户才能访问某个页面(其目的更明确),您希望仅允许某个页面访问某个页面;从你的问题很明显,它可能还没有经过身份验证的用户。



所以,目的还不清楚,我怀疑这是一个好主意。无论如何,你不能限制对页面的访问,也不能阻止任何用户在浏览器的地址栏中输入任何URL。



你可以做不同的事情。在ASP.NET代码中,您可以检查引荐来源网址: http://en.wikipedia.org/wiki/Referrer [ ^ ]。



http://msdn.microsoft.com/en -us / library / system.web.httprequest.urlreferrer.aspx [ ^ ]。



根据引荐来源,您的ASP.NET页面可以提供不同的内容,如果推荐人不是你所期望的,任何事情,甚至会产生错误404.

我希望其余部分都清楚。 :-)



-SA
Basically, you cannot limit access to any page on your site, if this page is accessible at all. Note that you do not require that only the authenticated user has access to some page (the purpose of it would be more clear), you want to allow access from some page only from a certain page; from your question it is clear that it could be not yet authenticated user.

So, the purpose of it is unclear, and I doubt that it is a good idea. Anyway, you cannot limit the access to the page, as well as you cannot prevent any user from typing any URL in the browser's address bar.

You can do different thing. In your ASP.NET code, you can check up the referrer URL: http://en.wikipedia.org/wiki/Referrer[^].

http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx[^].

Depending on the referrer, your ASP.NET page can provide different content, if the referrer is not what you expect, anything at all, even generate error 404.
I hope the rest of it is clear. :-)

—SA


当用户登录您的应用程序时设置会话变量用户名,然后当用户请求页面检查会话变量设置与否时如果没有设置然后重定向到登录页面



在主服务器中使用以下代码你的项目页面

When a user login to your application set a session variable with user name and then when the user requests a page check session variable set or not if it is not set then redirect to login page

Use the following code in the master page of you project
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["user"].ToString() == "")
    {
        Response.Redirect("~/Default.aspx");
    }
}


这篇关于如何才允许我的asp.net网站页面仅从登录页面打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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