ASP.NET 웹 페이지 - WebSecurity 개체


설명

WebSecurity 개체ASP.NET 웹 페이지 응용 프로그램에 대한 보안 및 인증을 제공합니다.

WebSecurity 개체를 사용하여 사용자 계정 생성, 사용자 로그인 및 로그아웃, 암호 재설정 또는 변경 등을 수행할 수 있습니다.


WebSecurity 개체 참조 - 속성

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

WebSecurity 개체 참조 - 메서드

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


WebSecurity 데이터베이스 초기화

코드에서 WebSecurity 개체를 사용하려면 먼저 WebSecurity 데이터베이스를 만들거나 초기화해야 합니다.

웹 루트에서 _AppStart.cshtml 이라는 페이지를 생성(또는 편집)합니다 .

파일 안에 다음 코드를 넣습니다.

_AppStart.cshtml

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

위의 코드는 웹 사이트(응용 프로그램)가 시작될 때마다 실행됩니다. WebSecurity 데이터베이스를 초기화합니다.

"사용자" 는 WebSecurity 데이터베이스(Users.sdf)의 이름입니다.

"UserProfile" 은 사용자 프로필 정보가 포함된 데이터베이스 테이블의 이름입니다.

"UserId" 는 사용자 ID(기본 키)를 포함하는 열의 이름입니다.

"이메일" 은 사용자 이름이 포함된 열의 이름입니다.

마지막 매개변수 true 는 사용자 프로필 및 멤버십 테이블이 존재하지 않는 경우 자동으로 생성되어야 함을 나타내는 부울 값이고, 그렇지 않으면 false 입니다.

true 는 데이터베이스 테이블 의 자동 생성을 나타내지 데이터베이스 자체는 자동으로 생성되지 않습니다. 존재해야 합니다.


WebSecurity 데이터베이스

UserProfile 테이블에는 사용자 ID(기본 키) 및 사용자 이름(이메일)과 함께 각 사용자에 대한 하나의 레코드가 포함되어 있습니다 .

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

멤버십 테이블 에는 사용자가 생성된 시기와 멤버십이 확인되었는지 여부(및 시기)에 대한 멤버십 정보가 포함됩니다.

이와 매우 유사합니다(일부 열은 표시되지 않음):

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

간단한 회원 구성

사이트가 ASP.NET 웹 페이지 멤버십 시스템 SimpleMembership 을 사용하도록 구성되지 않은 경우 WebSecurity 개체를 사용하여 오류가 발생할 수 있습니다 .

호스팅 공급자의 서버가 로컬 서버와 다르게 구성된 경우 발생할 수 있습니다. 이 문제를 해결하려면 사이트의 Web.config 파일에 다음 요소를 추가하세요.

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