ASP.NET Web Pages - WebSecurity 对象
- 上一页 Class WebPages
- 下一页 Database WebPages
描述
WebSecurity 对象提供为 ASP.NET Web Pages 应用程序提供安全和认证。
通过 WebSecurity 对象,您能够创建用户账户、登录和注销,重置或更改密码等等。
WebSecurity 对象参考手册 - 属性
属性 | 描述 |
---|---|
CurrentUserId | 获取当前用户的 ID。 |
CurrentUserName | 获取当前用户的名称。 |
HasUserId | 获取指示当前用户是否具有用户 ID 的值。如果是,返回 true。 |
IsAuthenticated | 获取当前用户的身份验证状态。 |
WebSecurity 对象参考手册 - 方法
属性 | 描述 |
---|---|
ChangePassword() | 更改用户的密码。 |
ConfirmAccount() | 确认帐户有效并激活该帐户。 |
CreateAccount() | 创建新的用户帐户。 |
CreateUserAndAccount() | 创建新的用户帐户。 |
GeneratePasswordResetToken() | 生成可以通过电子邮件发送给用户的密码重置令牌。 |
GetCreateDate() | 返回创建指定成员资格帐户的日期和时间。 |
GetPasswordChangeDate() | 返回最近更改指定成员资格帐户密码的日期和时间。 |
GetPasswordFailures SinceLastSuccess() |
Yan tukuna kwanan dakiya na kaiyan sami aro na mutummin dake zakiya. |
GetUserId() | A kan sami ID na mutummin zakiya. |
GetUserIdFrom PasswordResetToken () |
Return the user ID from the password reset token. |
InitializeDatabaseConnection() | Initialize the membership system by connecting to the database that contains user information. |
IsAccountLockedOut() | Indicate whether the specified membership account has been locked out due to too many failed password attempts. |
IsConfirmed() | Return a value indicating whether the user has been confirmed. |
IsCurrentUser() | Return a value indicating whether the username of the logged-in user matches the specified username. |
Login() | Log in the user. |
Logout() | Log out the user. |
RequireAuthenticatedUser() | If the user is not authenticated, set the HTTP status to 401 (Unauthorized). |
RequireRoles() | If the current user does not belong to the specified role, set the HTTP status code to 401. |
RequireUser() | If the current user is not the specified user, set the HTTP status to 401. |
ResetPassword() | Reset the password using the password reset token. |
UserExists() | Check if the user exists. |
Technical Data
Name | Value |
---|---|
Class | WebMatrix.WebData.WebSecurity |
Namespace | WebMatrix.WebData |
Assembly | WebMatrix.WebData.dll |
Kirƙira samarwa na WebSecurity Database
Kafin a yi amfani da jadambuntubar WebSecurity, a ƙirƙira ko ƙirƙira samarwa na database na WebSecurity.
A cikin shafin web na root, tsara ko kuma ka ƙaddamar da shafin _AppStart.cshtml.
A cikin wannan fayil, rubuta koda na uwa:
_AppStart.cshtml
@{ WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true); }
Yanar da yana fara kai a kowace lokaci, yana kai kwanan wata a cikin koda na uwa. Yana ƙirƙira samarwa na database na WebSecurity.
"Users" ɗanana shi sunan database na WebSecurity (Users.sdf).
"UserProfile" ɗanana shi sunan tashin da bayanai na tsarin samarwa na ɗan wasa na database.
"UserId" 包含用户 ID 的列的名称(主键)。
"Email" 是包含用户名称的列名。
最后一个参数 true 是一个逻辑值,指示应创建用户配置文件表和成员资格表(如果它们不存在),否则 false。
提示:尽管 true 指示自动创建数据库表,但数据库本身不会被自动创建。它必须存在。
WebSecurity 数据库
UserProfile 表中的每条记录代表一个用户,其中包括用户 ID (主键)和用户名称(email):
UserId | |
---|---|
1 | john@johnson.net |
2 | peter@peterson.com |
3 | lars@larson.eut |
Membership 表包含成员资格信息,比如用户何时创建以及是否(以及何时)确认成员资格。
类似这样(某些列未列出):
UserId | 创建日期 | 确认 令牌 |
是否 确认 |
最后的 密码错误 |
密码 | 密码更改 |
---|---|---|---|---|---|---|
1 | 12.04.2012 16:12:17 | NULL | True | NULL | AFNQhWfy.... | 12.04.2012 16:12:17 |
注释:如果您希望看到所有列和所有内容,请通过 WebMatrix 打开数据库,然后查看每张表。
简单的成员资格配置
如果您的站点未配置为使用 ASP.NET Web Pages 成员资格系统 SimpleMembership,那么可能会在使用 WebSecurity 对象时出现错误。
如果主机提供商的配置与您的本地服务器不同,就会发生错误。为了解决这个问题,请在站点的 Web.config 文件中添加以下元素:
- 上一页 Class WebPages
- 下一页 Database WebPages