如何使用表单身份验证将用户重定向到特定页面 [英] How to redirect a user to a specific page with forms authentication

查看:134
本文介绍了如何使用表单身份验证将用户重定向到特定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想配置应用程序并阻止用户直接访问应用程序中的任何页面而无需登录,但任何用户都可以访问网站主页。



但是当我运行主页,登录页面或网站的任何页面时,我收到此错误: - 无法访问请求的页面,因为页面的相关配置数据无效。



我找不到我弄错的地方。

有5个文件夹: -

1)AdminHome

2)第一页:包含网站的主页

3)注册:包含登录和注册页面

4)学生

5)老师



我发布了我的web.config文件。看看它。看看我在哪里犯错误,解决方案是什么。



我尝试过:



web.config



I want to configure the application and prevent the user from going directly to any page in the application without signing in but any user can access the websites homepage.

But when I run the homepage ,login page or any page of the website, I am getting this error:- The requested page cannot be accessed because the related configuration data for the page is invalid.

I can't find out where I am making mistake.
There are 5 folders :-
1) AdminHome
2) FIRST PAGE : contain homepage of the website
3) Registration : contain Login and signup page
4) Student
5) Teacher

I have posted my web.config file . have a look over it .show me where I am making mistake and what is the solution.

What I have tried:

web.config

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"

        providerName="System.Data.SqlClient" />
  </connectionStrings>

  <authentication mode="Forms">
    
    <forms loginUrl="/Registration/LoginPage.aspx">
      
    </forms>
    
  </authentication>
  

    <system.web>
      <compilation debug="true" targetFramework="4.5.2" />
      <httpRuntime targetFramework="4.5.2" />
    </system.web>
  
  <location path="FIRST PAGE">
      <system.web>
        <authorization>
          <allow users="*"/>
          
        </authorization>
      </system.web>
    </location>
  
  <location path="Registration">
      <system.web>
        <authorization>
          <allow users="?"/>
          
        </authorization>
      </system.web>
    </location>
  
  
    <location path="AdminHome">
      <system.web>
        <authorization>
          <allow users="admin"/>
          <deny users="*"/>
        </authorization>
      </system.web>
    </location>
  
  <location path="Student">
      <system.web>
        <authorization>
          <allow roles="Student"/>
          <deny users="*"/>
        </authorization>
      </system.web>
    </location>
  
<location path="Teacher">
      <system.web>
        <authorization>
          <allow roles="Teacher"/>
          <deny users="*"/>
        </authorization>
      </system.web>
    </location>

  <appSettings>

    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
    
  </appSettings>
  

</configuration>





错误





ERROR

HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.

Detailed Error Information:
Module	   IIS Web Core
Notification	   Unknown
Handler	   Not yet determined
Error Code	   0x80070032
Config Error	   The configuration section 'authentication' cannot be read because it is missing a section declaration
Config File	   \\?\E:\Way2Success\web.config
Requested URL	   http://localhost:53902/Registration/LoginPage.aspx
Physical Path	   
Logon Method	   Not yet determined
Logon User	   Not yet determined
Request Tracing Directory	   C:\Users\Dixit\Documents\IISExpress\TraceLogFiles\

Config Source:
   14: 
   15:   <authentication mode="Forms">
   16:     

More Information:
This error occurs when there is a problem reading the configuration file for the Web server or Web application. In some cases, the event logs may contain more information about what caused this error.
If you see the text "There is a duplicate 'system.web.extensions/scripting/scriptResourceHandler' section defined", this error is because you are running a .NET Framework 3.5-based application in .NET Framework 4. If you are running WebMatrix, to resolve this problem, go to the Settings node to set the .NET Framework version to ".NET 2". You can also remove the extra sections from the web.config file.
View more information »

推荐答案

HI,probelm可能在IIS中,请查看以下链接以解决您的疑问。



http://www.aspdotnet-suresh.com/2011/05/requested-page-cannot-be-accessed.html [ ^ ]



< a href =http://www.codeproject.com/Questions/324900/The-requested-page-cannot-be-accessed-beause-the>无法访问请求的页面,因为页面的相关配置数据是无效。 [ ^ ]



c# - 无法访问请求的页面,因为页面的相关配置数据无效错误 - Stack Overflow [ ^ ]
probelm may be in IIS , please check these links below to solve your query.

http://www.aspdotnet-suresh.com/2011/05/requested-page-cannot-be-accessed.html[^]

The requested page cannot be accessed because the related configuration data for the page is invalid.[^]

c# - The requested page cannot be accessed because the related configuration data for the page is invalid error - Stack Overflow[^]


< authentication>部分配置应该在< system.web>内。部分







刚编辑完web.config:



The <authentication> part of the configuration should be inside the <system.web> section



Just edited the web.config:

<system.web>
   <authentication mode="Forms">
       <forms loginUrl="/Registration/LoginPage.aspx">
       </forms>
   </authentication>
   <compilation debug="true" targetframework="4.5.2" />
   <httpruntime targetframework="4.5.2" />
</system.web>


解决此问题的一种简单方法是在登录页面中使用身份验证过程在页面加载时检查来自AdminHome,学生,教师的页面中的身份验证详细信息。



在登录页面中验证成功后,将用户按角色重定向到相应的文件夹/页面。在重定向之前,在会话和cookie变量中保存所需的用户凭据。然后使用会话和cookie中的值,在上述文件夹中的相关页面上交叉检查用户。如果您使用母版页,则需要在母版页加载事件中仅检查一次所需的验证详细信息。如果用户详细信息不匹配,只需将用户重定向到登录页面或主页。



以下代码片段用于登录;



A simple way to solve this problem is using authentication process in login page and checking authentication details in pages from AdminHome, Student, Teacher at page load.

When authentication is successful in login page, redirect user as per role to corresponding folder/page. Before redirecting, save required user credentials in session and cookie variables. Then cross check user at related pages in above mentioned folder using this values from session and cookies. If you are using master page, then you need to check required authentication details only once in master page load event. If user details does not match, simply redirect user to either login page or home page.

Following code snippet is used for login;

Session["DynamicID"] = dynamic_id;
               Session["EmailID"] = txtbx_EmailID.Text.Trim();
               Session["DispName"] = GetDispName();

               app_cookie = new HttpCookie("App", dynamic_id);

               Response.AppendCookie(app_cookie);
               Response.RedirectPermanent("~/Dept/Default.aspx",true);





以下代码块用于母版加载事件;





Following code block is used at master page load event;

ses_autoid = Session["DynamicID"].ToString();
           cook_autoid = Request.Cookies.Get("App").Value.ToString();
           if (ses_autoid == cook_autoid)
           {
               Label_User.Text = "Display name: " + Session["DispName"].ToString();
               Label_User1.Text = Session["DispName"].ToString();
           }
           else {
               Response.RedirectPermanent("~/Common/LogOut.aspx", true);
           }


这篇关于如何使用表单身份验证将用户重定向到特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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