Trang Web ASP.NET - Đối tượng Bảo mật Web


Sự miêu tả

Đối tượng Bảo mật Web cung cấp bảo mật và xác thực cho các ứng dụng Trang Web ASP.NET.

Với đối tượng WebSecurity, bạn có thể tạo tài khoản người dùng, đăng nhập và đăng xuất người dùng, đặt lại hoặc thay đổi mật khẩu, v.v.


Tham chiếu đối tượng WebSecurity - Thuộc tính

Properties Description
CurrentUserId Gets the ID for the current user
CurrentUserName Gets the name of the current user
HasUserId Returns true if the current has a user ID
IsAuthenticated Returns true if the current user is logged in

Tham chiếu đối tượng bảo mật Web - Phương thức

Method Description
ChangePassword() Changes the password for a user
ConfirmAccount() Confirms an account using a confirmation token
CreateAccount() Creates a new user account
CreateUserAndAccount() Creates a new user account
GeneratePasswordResetToken() Generates a token that can be sent to as user by email
GetCreateDate() Gets the time the specified membership was created
GetPasswordChangeDate() Gets the date and time when password was changed
GetUserId() Gets a user ID from a user name
InitializeDatabaseConnection() Initializes the WebSecurity system (database)
IsConfirmed() Checks if a user is confirmed
IsCurrentUser() Checks if the current user matches a user name
Login() Logs the user in by setting a token in the cookie
Logout() Logs the user out by removing the token cookie
RequireAuthenticatedUser() Exits the page if the user is not an authenticated user
RequireRoles() Exits the page if the user is not a part of the specified roles
RequireUser() Exits the page if the user is not the specified user
ResetPassword() Changes a user's password using a token
UserExists() Checks if a given user exists


Khởi tạo Cơ sở dữ liệu WebSecurity

Bạn phải tạo hoặc khởi tạo cơ sở dữ liệu WebSecurity trước khi có thể sử dụng đối tượng WebSecurity trong mã của mình.

Trong thư mục gốc của web, hãy tạo một trang (hoặc chỉnh sửa trang) có tên _AppStart.cshtml .

Đặt mã sau vào bên trong tệp:

_AppStart.cshtml

@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);
}

Đoạn mã trên sẽ chạy mỗi khi trang web (ứng dụng) khởi động. Nó khởi tạo cơ sở dữ liệu WebSecurity.

"Người dùng" là tên của cơ sở dữ liệu WebSecurity (Users.sdf).

"UserProfile" là tên của bảng cơ sở dữ liệu có chứa thông tin hồ sơ người dùng.

"UserId" là tên của cột chứa ID người dùng (khóa chính).

"Email" là tên của cột chứa tên người dùng.

Tham số cuối cùng true là một giá trị boolean chỉ ra rằng hồ sơ người dùng và các bảng thành viên sẽ được tạo tự động nếu chúng không tồn tại, nếu không thì sai .

Mặc dù true cho biết việc tạo bảng cơ sở dữ liệu tự động , nhưng bản thân cơ sở dữ liệu sẽ không được tạo tự động. Nó phải tồn tại.


Cơ sở dữ liệu WebSecurity

Bảng UserProfile chứa một bản ghi cho mỗi người dùng, với ID người dùng (khóa chính) và tên người dùng (email):

UserId Email
1 [email protected]
[email protected]
3 [email protected]

Bảng Thành viên sẽ chứa thông tin thành viên về thời điểm người dùng được tạo và nếu (và khi) tư cách thành viên được xác nhận.

Giống như thế này (một số cột không được hiển thị):

User
Id
Create
Date
Confirmation
Token
Is
Confirmed
Last
Password
Failure
Password Password
Change
1 12.04.2012 16:12:17 NULL True NULL AFNQhWfy.... 12.04.2012 16:12:17

Cấu hình thành viên đơn giản

Bạn có thể gặp lỗi khi sử dụng đối tượng WebSecurity, nếu trang web của bạn không được định cấu hình để sử dụng hệ thống thành viên Trang web ASP.NET SimpleMembership .

Điều này có thể xảy ra nếu máy chủ của nhà cung cấp dịch vụ lưu trữ được định cấu hình khác với máy chủ cục bộ của bạn. Để khắc phục sự cố này, hãy thêm phần tử sau vào tệp Web.config của trang web:

<appSettings>
<add key="enableSimpleMembership" value="true" />
</appSettings>