简体   繁体   English

如何为aspx页面添加控制器?

[英]How to add a controller for an aspx page?

This is the common issue ,that i have faced. 这是我面临的常见问题。 when the user clicks one of the menu item, if the user didn't login ,I want to redirect to Login page itself. 当用户单击菜单项之一时,如果用户未登录,我想重定向到“登录”页面本身。 Which is the efficient way to do this? 哪种有效的方法可以做到这一点?

In Jsp we have a controller for every page, I dont know what we use in asp.net. 在Jsp中,每个页面都有一个控制器,我不知道我们在asp.net中使用了什么。

The easiest way is using web.config file. 最简单的方法是使用web.config文件。 Have a look at the documentation for the <authentication> section. 查看<authentication>部分的文档。 You might end up with settings like this: 您可能最终会得到如下设置:

<!-- Anonymous users denied, exceptions in the Location sections below -->
<authorization>
    <deny users="?" />
</authorization>
<authentication mode="Forms">
    <forms loginUrl="~/LogOn.aspx" timeout="2880" name=".ASPXAUTH_CMS" protection="All" />
</authentication>

<!-- Allow all users to see the login screen -->
<location path="~/LogOn.aspx">
    <system.web>
        <authorization>
            <allow users="*" />
        </authorization>
    </system.web>
</location>

If the user is not logged on they will be redirected to "Logoin.aspx" and once they've successfully authenticated they will be redirected back to the original URL. 如果用户未登录,他们将被重定向到“ Logoin.aspx”,一旦成功通过身份验证,他们将被重定向回原始URL。

If you're not using ASP.NET Membership and using your own variables cookies etc for logging in the user then you can override the Page Class and derive all your pages from this Page class. 如果您不使用ASP.NET成员资格,也不使用自己的变量cookie等来登录用户,则可以重写Page类并从此Page类派生所有页面。 Then all your pages construct the Page class with a constructor whether the page needs login or not. 然后,无论页面是否需要登录,所有页面都使用构造函数构造Page类。 The Page class checks this variable while constructing and redirects the url to the login page when the page requires login and the credentials are not present. Page类在构造时会检查此变量,并在该页面需要登录且不存在凭据时将URL重定向到登录页面。

尝试Response.Redirect("TheLoginPage.aspx");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM