Hướng dẫn bảo mật cho từng folder in asp.net

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

Control authorization permissions in an ASP.NET application

  • Bài viết
  • 01/25/2022

Trong bài viết này

This update introduces how to apply the <location> tag to the Web.config file to configure access to a specific file and folder.

Original product version: ASP.NET Original KB number: 316871

Summary

Use this step-by-step guide to apply the <location> tag to the Web.config file to configure access to a specific file and folder.

When using forms-based authentication in ASP.NET applications, only authenticated users are granted access to pages in the application. Unauthenticated users are automatically redirected to the page specified by the loginUrl attribute of the Web.config file where they can submit their credentials. In some cases, you may want to permit users to access certain pages in an application without requiring authentication.

Configure access to a specific file and folder

  1. Set up forms-based authentication. For more information, see How to implement Forms-Based Authentication in Your ASP.NET Application by Using C# .NET.
  2. Request any page in your application to be redirected to Logon.aspx automatically.
  3. In the Web.config file, type or paste the following code.

    This code grants all users access to the Default1.aspx page and the Subdir1 folder.

    <configuration>

    <system.web>  
        <authentication mode="Forms" >  
            <forms loginUrl="login.aspx" name=".ASPNETAUTH" protection="None" path="/" timeout="20" >  
            </forms>  
        </authentication>  
        <! This section denies access to all files in this application except for those that you have not explicitly specified by using another setting. >  
        <authorization>  
            <deny users="?" />  
        </authorization>  
    </system.web>  
    <! This section gives the unauthenticated user access to the Default1.aspx page only. It is located in the same folder as this configuration file. >  
    <location path="default1.aspx">  
        <system.web>  
            <authorization>  
                <allow users ="" />  
            </authorization>  
        </system.web>  
    </location>  
    <! This section gives the unauthenticated user access to all of the files that are stored in the Subdir1 folder. >  
    <location path="subdir1">  
        <system.web>  
            <authorization>  
                <allow users ="" />  
            </authorization>  
        </system.web>  
    </location>  
    
    </configuration>

    Users can open the Default1.aspx file or any other file saved in the subdir1 folder in your application. They won't be redirected automatically to the Logon.aspx file for authentication.

Repeat Step 3 to identify any other pages or folders for which you want to permit access by unauthenticated users.

Trên diễn đàn cũng như 1 số báo chí đã chia sẻ nhiều cách cài đặt mật khẩu cho folder hay còn gọi là khóa thư mục folder. Tuy nhiên, để cài pass cho folder SecurityBox sẽ hướng dẫn các bạn từ cách đơn giản đến phức tạp.

\>> PART 2 – REVIEW CÁC PHẦN MỀM BẢO VỆ THƯ MỤC HAY NHẤT

Thao tác thực hiện như sau:

Bước 1: Đặt tên cho folder bạn cần giải nén (nếu chưa có).

Hướng dẫn bảo mật cho từng folder in asp.net

Ví dụ tên thư mục hiện tại của mình là SecurityBox.vn

Bước 2: Click chuột phải vào thư mục => Chọn “Add to archive…”

Bước 3: Chọn thẻ “Advanced” hoặc “General” => nhìn sang bên phải, click “Set password”.

Hướng dẫn bảo mật cho từng folder in asp.net

Bước 4: Nhập password vào ô “Enter password” (nhớ đánh pass cả 2 ô) > ok > ok, tiếp tục nhập lại password vào ô dưới để xác minh mật khẩu. Sau đó nhấp OK là được.

Hướng dẫn bảo mật cho từng folder in asp.net

PHẦN 2 – BẢO MẬT THƯ MỤC FOLDER BẰNG ĐOẠN MÃ

Tiếp theo series “Bảo mật thư mục folder từ đơn giản đến nâng cao”. SecurityBox sẽ hướng dẫn các bạn cách ‘bảo vệ thư mục folder bằng đoạn mã.

Bước 1: Tạo một thư mục XYZ để đặt các tập tin cần khóa vào bên trong. Nếu đã tạo sẳn, bạn có thể bỏ qua bước này.

Hướng dẫn bảo mật cho từng folder in asp.net

Ví dụ mình đặt tên folder là SecurityBox.vn

Bước 2: Mở thư mục vừa tạo, click chuột phải và chọn New => Text Document

Hướng dẫn bảo mật cho từng folder in asp.net

Bước 3: Mở tập tin mới vừa tạo ra, sau đó dán vào đoạn mã dưới này nhưng hãy thay đổi giá trị PASSWORD_IN_HERE thành mật khẩu mà bạn muốn đặt nhé.

Đây là đoạn mã:

cls

@ECHO OFF

title Folder Private

if EXIST “Genk Locker” goto UNLOCK

if NOT EXIST Private goto MDLOCKER

:CONFIRM

echo Are you sure you want to lock the folder(Y/N)

set/p “cho=>”

if %cho%==Y goto LOCK

if %cho%==y goto LOCK

if %cho%==n goto END

if %cho%==N goto END

echo Invalid choice.

goto CONFIRM

:LOCK

ren Private “Genk Locker”

attrib +h +s “Genk Locker”

echo Folder locked

goto End

:UNLOCK

echo Enter password to unlock folder

set/p “pass=>”

if NOT %pass%== PASSWORD_IN_HERE goto FAIL

attrib -h -s “Genk Locker”

ren “Genk Locker” Private

echo Folder Unlocked successfully

goto End

:FAIL

echo Invalid password

goto end

:MDLOCKER

md Private

echo Private created successfully

goto End

:End

Bước 4: Lưu fle lại với tên và định dạng là “locker.bat”. Sau đó, xóa tập tin TXT mà mình đã tạo. Tiếp click đôi chuột vào tập tin locker.bat để nó hoạt động. Khi đó một thư mục mới có tên “Private” sẽ hiện ra.

Hướng dẫn bảo mật cho từng folder in asp.net

Bước 5: Chuyển các các dữ liệu cần bảo mật vào thư mục folder Private. Khi kết thúc, click 2 lần đúp chuột vào tập file locker.bat. Ngay sau đó, một hộp thoại hiện ra với đoạn text hỏi bạn có đồng ý “khóa folder này không” (Are you sure you want to lokc the folder (Y/N) => nhập “Y” => chọn ENTER.

Hướng dẫn bảo mật cho từng folder in asp.net

Kết thúc bước trên, folder Private sẽ biến mất. Khi muốn truy cập vào folder Private, click 2 lần chuột vào tập tin locker.bat và tiến hành nhập mật khẩu đã đặt trong cửa sổ dòng lệnh => sau đó ấn phím ENTER.

Nếu bạn muốn ấn tập tin locker.bat bằng cách chuột phải cllick vào tệp tin locker.bar => chọn properties => click chọn “Hidden” => click OK.

Hướng dẫn bảo mật cho từng folder in asp.net

LƯU Ý: Nếu bạn muốn thay đổi mật khẩu bất kỳ lúc nào, chỉ cần mở folder bạn đã đặt password trước đó, chọn tệp tin “locker.bat” => Chuột phải chọn Edit => Đặt mật khẩu mới.

Cách đặt mật khẩu cho folder này khá đơn giản phải không? Thay vì tải phần mềm khóa folder, hãy khóa thư mục trên máy tính, PC của bạn bằng cách dùng đoạn mã nhé!