(翻译)ASP.NET Web Pages(Razor) WebSecurity 基本用法

ASP.NET Web Pages(Razor)有关于security/login的基础设置,能够自动处理相关过程。首先,membership信息(例如,用户名、密码)是存储在你的网站的数据库中。Razor包含了membership provider,用于该数据库管理的详细处理。事实上,你可以不用知道登录信息是如何、在何地被处理的。本教程是基于你乐于了解藏在引擎背后的细节。

Razor还包括Websecurity助手、Membership和Roles对象,提供管理用户的相关方法。例如,创建用户、登录和注销方法。本教程仅包括一些基本功能的练习。

1、初始化membership

在网站的根目录,创建_AppStart.cshtml。代码

在文件中,调用WebSecurity.InitializeDatabaseConnection 方法。

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

参数值说明:

"TestMembership":这是提供给ASP.NET存储用户信息的数据库的名称,该数据库必须是已存在的,当用户信息相关的表不存在时,初始化方法将会自动创建相关的表。

"UserProfile":存储user信息的表名,具体见下文。

"UserId":user-profile表中用户的主键名,具体见下文。

"Email":user-profile表中用户名对应的列名(假定为email地址)。

初始化的作用:

membership系统被设计为可以与已存在用户信息的数据库进行集成。例如已存在联系人列表或者雇员表,初始化代码用可指定包含用户Id、用户名信息的表名和列名,membership系统稍后会用到这些信息。

membership系统实际上是将"profile"数据和"membership"数据进行区分的。profile数据包括用户名、ID,以及其他必要的信息(例如地址)。相反,membership数据是membership系统所需要的关于安全方面的详尽资料,例如 密码hash、上次修改密码时间等。这些信息不太可能存在与profile数据库表中,或者你也不想存储在那里。总之,通过profile"数据和"membership"数据的分割,ASP.NET很容易使用已存在的用户数据库。

2、创建主页Home.cshtml。代码

在网站根目录创建页面 Home.cshtml。

添加指向Login页面的链接。

添加指向Register页面的链接。

在页面代码中,调用 WebSecurity.IsAuthenticated 来判断用户是否已登录。如果返回true,则显示当前的用户名(WebSecurity.CurrentUserName),并且添加指向Logout页面的链接,否则显示指向Login页面的链接。

@if(WebSecurity.IsAuthenticated)
{
  <p>Welcome, @WebSecurity.CurrentUserName</p>
  <p><a href="@Href("~/logout")">Log out</a></p>
}
else
{
  <p><a href="@Href("~/Login")">Log in</a> | 
  <a href="@Href("~/Register")">Register</a></p>
}

在实际应用中,使用Layout布局和其他方式等可重用的代码方式来显示Login链接和当前用户名。
3、创建注册页面Register.cshtml。代码

简单的注册页面可以输入用户名(可以是电子邮件)和密码,通常让用户输入两次密码以确认输入。

在网站根目录创建页面Register.cshtml。

添加用于输入用户名和密码(X2)的文本框(<input>元素),并添加提交按钮。

在PostBack处理中,通过调用WebSecurity.UserExists来确定用户名未被使用。

调用WebSecurity.CreateUserAndAccount创建membership数据。

if(WebSecurity.UserExists(username))
{
    errorMessage = String.Format("User '{0}' already exists.", 
        username);
}
else
{
    WebSecurity.CreateUserAndAccount(username, password,
        null, false);
    WebSecurity.Login(username, password, true);
    errorMessage = String.Format("{0} created.", username);
}

更多:

比较密码的两次输入,确保相同。

在提交处理代码中,调用WebSecurity.Logout强制将当前的用户登录Logout。

在提交处理代码中,首先调用WebSecurity.IsAuthenticated判断当前用户是否已登录,若是则显示错误并跳过注册过程。

if(WebSecurity.IsAuthenticated)
{
   errorMessage = String.Format("You are already logged in." + 
       " (User name: {0})", WebSecurity.CurrentUserName);
}

当成功创建membership用户后,调用WebSecurity.Login自动登录。

Redisplay the user's entry in the user name text box (useful if there was an error so that they can see what they entered). Due to HTML constraints, you can't do this with passwords, even if you wanted to.

在实际应用中,使用SSL来加密浏览器与服务器间的通信。具体见:http://www.microsoft.com/web/post/securing-web-communications-certificates-ssl-and-https

添加验证码处理,确保实际是人工进行注册的。




原文地址:http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=2240

转载于:https://my.oschina.net/u/723915/blog/335214

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值