当前位置:首页 > 使用Forms Authentication实现用户注册、登录 (三)用户实体替换

使用Forms Authentication实现用户注册、登录 (三)用户实体替换

点击次数:1425  更新日期:2010-12-29
\n

IPrincipal和IIdentity


\n

  通过查阅文档,我们可以看到HttpContext.User属性的类型是IPrincipal接口。然而我们知道,接口通常是不能直接访问的,其背后必定隐藏了一个实现了该接口的对象。那么这个实际对象的类型是什么呢?


\n

  让我们在前面示例的MasterPage的Page_Init方法上加一个断点,再次运行程序,可以得到HttpContext.User属性的真正类型是System.Security.Principal.GenericPrincipal。


\n

  查看IPrincipal接口的定义,可以看到它只有一个公开属性——Identity,其类型是这里要提到的另外一个重要接口IIdentity。通过上面的断点跟踪,我们还能知道对于GenericPrincipal而言,其Identity属性的实际类型是GenericIdentity,也是位于System.Security.Principal命名空间中。


\n

  由此,我们引出了.NET Framework中关于Principal(实体)的整个类型系统。所有这些类型都位于mscorlib.dll程序集中,由此也可以看出,这套模型隶属于整个系统的基石。


\n


\n

实现自己的IPrincipal


\n

  要想用自己的实体对象替换HttpContext.User,就必须让自己的实体对象实现IPrincipal接口;通常还必须伴随着实现IIdentity接口。


\n

  目前系统中有的是一个数据实体对象。一般而言,实现IPrincipal接口有一下两种方式——


\n

编写单独的类型实现IPrincipal接口,并在其中包含数据实体对象;


\n

修改数据实体对象使其实现IPrincipal接口。


\n

  对于这两种方式而言,其Identity属性可以通过以下三种方式实现——


\n

使用.NET Framework提供的GenericIdentity;


\n

创建自定义的类,实现Identity接口;


\n

修改数据实体对象或自定义的实体类,让它们同时实现IPrincipal和IIdentity接口。


\n

  对于简单的应用程序而言,通常可以修改数据实体对象,使其同时实现IPrincipal和IIdentity接口。而就复杂的分层架构应用程序,则建议在逻辑层创建分别实现了IPrincipal和IIdentity接口的对象。本文的示例 明显属于前一种情况,因此我们考虑修改作为数据实体类的UserObject类,让其实现两个接口。以下是修改完毕的UserObject类:


\n


\n

public class UserObject : IPrincipal, IIdentity


\n

{


\n

/// <summary>


\n

/// 用户名。


\n

/// </summary>


\n

public string Name;


\n


\n

/// <summary>


\n

/// 密码散列值。


\n

/// </summary>


\n

public string PasswordHash;


\n


\n

/// <summary>


\n

/// 密码salt值。


\n

/// </summary>


\n

public string PasswordSalt;


\n


\n

#region IIdentity Members


\n


\n

public string AuthenticationType


\n

{


\n

get


\n

{


\n

return “Froms”;


\n

}


\n

}


\n


\n

public bool IsAuthenticated


\n

{


\n

get


\n

{


\n

return true;


\n

}


\n

}


\n


\n

string IIdentity.Name


\n

{


\n

get


\n

{


\n

return this.Name;


\n

}


\n

}


\n


\n

#endregion


\n


\n

#region IPrincipal Members


\n


\n

public IIdentity Identity


\n

{


\n

get


\n

{


\n

return this;


\n

}


\n

}


\n


\n

public bool IsInRole(string role)


\n

{


\n

return false;


\n

}


\n


\n

#endregion


\n

}


\n


\n

  首先我们来看一下对IIdentity接口的实现。该接口要求三个属性——AuthenticationType、IsAuthenticated和Name。AuthenticationType表示该用户标识所使用的验证类型,这里返回的是“Forms”;IsAuthenticated属性表示当前用户是否已经通过验证(即是否已登录。在这个例子里,我们只针对已登录用户进行实体替换,所以这个属性总是返回true。通常,实际的Web应用程序编写时还有一种习惯,就是为未登录用户(称之为匿名用户)也提供一个用户实体对象,此时就需要为IsAuthenticated提供逻辑,判断用户是否已通过验证了。最后IIdentity接口还要求对象提供一个Name属性,在这里,由于已经存在了Name字段,因此才用“显示接口实现”来提供Name属性,返回对象自身的Name字段即可。


\n

  接下来我们看一下IPrincipal接口的实现。该接口要求提供一个Identity属性和一个IsInRole方法。由于UserObject类本身已经实现了IIdentity接口,因此在Identity属性中直接reutren this即可。因为我们这个示例不涉及用户分组(角色)方面的技术,因此IsInRole方法总是返回false。


\n


\n

用户实体替换


\n

  用户实体替换即使用我们自己编写的类型的实例来替换HttpContext.User属性。实体替换应该发生在HttpApplication的PostAuthenticateRequest事件发生时,因为此时ASP.NET已经从客户端得到了用户凭证Cookie并进行了解密和校验。


\n

  我们既可以编写一个HttpModule来处理PostAuthenticateRequest事件,也可以在Global..asax文件中添加时间处理器。这里为了简单,我们选择在Global.asax中添加如下事件处理器:


\n


\n

void Application_PostAuthenticateRequest(object sender, EventArgs e)


\n

{


\n

HttpApplication app = (HttpApplication)sender;


\n

if(app.Context.User.Identity.Name != “”) // 仅在已登录时替换


\n

{


\n

UserObject user = DataAccess.GetUserByName(app.Context.User.Identity.Name);


\n

app.Context.User = user;


\n

Thread.CurrentPrincipal = user;


\n

}


\n

}


\n


\n

  在这里我们首先进行了判断,如果用户已登录,才进行实体替换。当然你也可以选择未未登录用户也提供一个匿名用户实体。


\n

  接下来,我们通过本来已经存放在HttpContext.User.Identity中的用户标识得到了数据实体对象,然后分别将其赋予HttpContext.User和Thread.CurrentPrincipal。


\n

  至此,我们的示例代码就完工了。没有提到的是,完成了这一步之后,你就可以通过类似下面的代码在任何可以访问到HttpContext的地方获取用户实体了:


\n


\n

UserObject user = HttpContext.Current.User as UserObject;


\n

if(user != null)


\n

{


\n

// 可以使用user


\n

}


\n

else


\n

{


\n

// 用户未登录


\n

}


\n


\n

  需要注意,由于在这里我们仅对已登录用户进行了用户实体替换,所以代码使用as进行类型转换并结合if语句进行判断是必需的。


\n


\n

小结


\n

  好吧,这一部分说的是用户实体替换。

\n