Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不允许用户在未登录的情况下访问页面/URL_C#_Asp.net_Webforms - Fatal编程技术网

C# 不允许用户在未登录的情况下访问页面/URL

C# 不允许用户在未登录的情况下访问页面/URL,c#,asp.net,webforms,C#,Asp.net,Webforms,我是ASP.net新手,但我正在自学!我正在测试和学习ASP.NET中的登录和注销功能。实际上,我的问题是,我有一个简单的页面,即Default.aspx,用于登录: <body> <form id="form1" runat="server"> <div> <h1>Please Sign in</h1> UserName: <asp:TextBox id="uname" runat="server"&g

我是ASP.net新手,但我正在自学!我正在测试和学习ASP.NET中的登录和注销功能。实际上,我的问题是,我有一个简单的页面,即Default.aspx,用于登录:

<body>
<form id="form1" runat="server">
<div>
    <h1>Please Sign in</h1>
    UserName:
    <asp:TextBox id="uname"  runat="server"></asp:TextBox>
    <br/>
    Password:
    <asp:TextBox id="upass"  runat="server"></asp:TextBox>
    <br/>
    <asp:Button id="but"  runat="server" text="signup" OnClick="but_Click"/>
    <br/>
    <asp:Label ID ="lblInformation" runat ="server" ForeColor ="Red"/>
</div>
</form>
</body>
成功登录后;将重定向到Home.aspx作为:

 <body>
   <form id="form1" runat="server">
     <div>
       <h1>Hello User</h1>
        <asp:Button ID="but" OnClick="but_Click" text="signout" runat="server"/>
     </div>
   </form>
 </body>
问题 问题是,登录后,如果我复制Home.aspx页面URL(登录后登录的页面),然后粘贴到浏览器搜索栏并按enter键,我就可以在没有登录的情况下看到它

我的意思是,如果我的用户没有登录,我希望他限制登陆Home.aspx

所以问题是,如果我的用户没有登录,我如何限制他查看Home.aspx页面,因为即使我没有登录,我也可以通过将Home.aspx URl复制到浏览器中来查看页面

对不起,我的英语不是来自英国,我只是在自学asp.net


感谢
页面中的事件检查以获取授权

if (!User.Identity.IsAuthenticated)
{
    Response.Redirect("~/Login.aspx");
}

页面加载
事件检查以获取授权

if (!User.Identity.IsAuthenticated)
{
    Response.Redirect("~/Login.aspx");
}

最简单的方法可能是在Home.aspx的页面加载方法add中


如果(!Request.IsAuthenticated){FormsAuthentication.RedirectToLogin();}

最简单的方法可能是在Home.aspx的页面加载方法中添加


如果(!Request.IsAuthenticated){FormsAuthentication.RedirectToLogin();}

让ASP.Net为您完成这项工作;您可以通过web.config来控制这一点

您可以将以下内容添加到
部分:

  <!-- Specify that only authenticated users are allowed to access pages by default. 
   Those that anonymous users can access will be specified separately. -->
  <authorization>
    <deny users="?" />
  </authorization>

然后,您可以添加以下条目以允许未登录用户访问default.aspx:

<!-- Specify those files that all users can access, even if they aren't logged in -->
<location path="Default.aspx">
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
</location>

让ASP.Net为您完成工作;您可以通过web.config来控制这一点

您可以将以下内容添加到
部分:

  <!-- Specify that only authenticated users are allowed to access pages by default. 
   Those that anonymous users can access will be specified separately. -->
  <authorization>
    <deny users="?" />
  </authorization>

然后,您可以添加以下条目以允许未登录用户访问default.aspx:

<!-- Specify those files that all users can access, even if they aren't logged in -->
<location path="Default.aspx">
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
</location>


您能告诉我User.Identity.IsAuthenticated与Request之间的区别吗。IsAuthenticated@JavaNerd没有区别。参考这里好的,谢谢@CodetrixStudio的帮助,这是我一直在寻找的,再次感谢!不客气,别忘了用最有帮助的答案来标记你的问题。尽管我可以花2美分在硬编码上,但登录页面是次优的方法。使用FormsAuthentication类可以从页面外部对其进行配置,因此如果将来发生更改,则不必在代码中进行任何更改。编辑:假设使用的是FormsAuthentication。您能告诉我User.Identity.IsAuthenticationed和Request之间的区别吗。IsAuthenticated@JavaNerd没有区别。参考这里好的,谢谢@CodetrixStudio的帮助,这是我一直在寻找的,再次感谢!不客气,别忘了用最有帮助的答案来标记你的问题。尽管我可以花2美分在硬编码上,但登录页面是次优的方法。使用FormsAuthentication类可以从页面外部对其进行配置,因此如果将来发生更改,则不必在代码中进行任何更改。编辑:假设使用的是FormsAuthentication。您能告诉我User.Identity.IsAuthenticationed和Request之间的区别吗。IsAuthenticated@JavaNerd-Request.IsAuthenticated无论使用何种身份验证方案(如FormsAuthentication、WindowAuth等)都能正常工作,具体取决于您的要求,这通常是一种更能证明未来的方法。编辑:嘿,我学到了一些东西。根据CodetrixStudio的链接,它们在功能上似乎是等效的。您能告诉我User.Identity.IsAuthenticated和Request之间的区别吗。IsAuthenticated@JavaNerd-Request.IsAuthenticated无论使用何种身份验证方案(如FormsAuthentication、WindowAuth等)都能正常工作,具体取决于您的要求,这通常是一种更能证明未来的方法。编辑:嘿,我学到了一些东西。根据CodetrixStudio的链接,它们似乎在功能上是等价的。