簡體   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