繁体   English   中英

如何使之只有经过身份验证的用户才能访问ASP .NET项目中的页面

[英]How to make it so that only authenticated users can access a page in an ASP .NET project

我想做的是,这样在登录到站点后,应用程序会自动将用户重定向到应用程序的默认页面。 另外,重要的是,用户必须先登录才能访问登录网页以外的其他内容。我已经坚持了三天。 我们有一个可怕的教授,他没有正确地做他的文档(我必须用Google搜索所有内容,而不是在脚本中放置内容)

用户的凭据在Web.config文件中被硬编码为键值。 它们不会针对数据库进行检查,而只会针对硬编码的字符串进行检查。

这是我的代码:

登录页面的设计(它是工具箱中的Login元素,不是手动构建的东西)。

这是该页的代码: Login.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace LV1___Kalkulator
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected bool ValidateUser(String username, String password)
        {
            if (Login1.UserName == "tstipic" && // ConfigurationManager.AppSettings["korisnickoime"] &&
               Login1.Password == "password") //ConfigurationManager.AppSettings["sifra"])
            {
                return true;
            }
            else return false;
        }

        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            if (e.Authenticated)
            {
                Response.Redirect("Dafault.aspx");
            }
            if (ValidateUser(Login1.UserName, Login1.Password))
            {
                Response.Redirect("Dafault.aspx");
            }
            else
            {
                e.Authenticated = false;
            }
        }
    }
}

这是我的web.config文件:

<?xml version="1.0" encoding="utf-8"?>

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

    <compilation debug="true" targetFramework="4.6.1"/>
    <httpRuntime targetFramework="4.6.1"/>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
    <add key="korisnickoime" value="user"/>
    <add key="sifra" value="pass"/>
    </appSettings>
  <connectionStrings>
    <add name="konekcijaNaBazu"
       connectionString="Provider=Microsoft.Jet.OLEDB.4.0;
       Data Source=|DataDirectory|\ASP_Database.mdb"
       providerName="System.Data.OleDb"/>  
  </connectionStrings>
</configuration>

编辑:我已经实现了阿尔伯特·D·卡洛尔所建议的。 这似乎是朝着正确方向迈出的一步,但我仍在经历相同的结果。 我输入了正确的凭据,仅能重新显示登录页面。

假设您启用了安全性,那么您需要在配置文件中执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
       <authorization>
        <deny users="?"/>
       </authorization>
    </system.web>
</configuration>

以上含义和含义:除非经过身份验证,否则请勿允许任何人访问网页。 如果未通过身份验证,则Web服务器将自动将用户重定向到您的登录页面。 结果是除非登录,否则无法点击任何网页。 如果他们手动输入URL,则Web服务器会将它们重定向到登录页面,因为那是您设置为登录页面的页面。 web.config中的设置是

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login.aspx" defaultUrl="~/Default.aspx" />
</authentication>

因此,以上设置了默认网页,但ALSO设置了每个人如果尝试未经身份验证即访问网页都将重定向到的页面。

暂无
暂无

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

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