关于用户登录、用户角色和profile,如何用Asp.net Membership和Forms认证模式登录用户获得角色和profile,如何创建RiaService的authentication domain service,在VS有个Silverlight的BusinessApplication模板,可以搞定以上问题。具体可以参考MSDN:Walkthrough: Using Authentication Service with Silverlight Business Application,以及WCF Ria Service:Authentication, Roles, and Profiles这两篇文章。
Authentication, Roles, Profiles
- 身份验证 - 验证用户的凭据,并将该用户标记为已登录或已注销。
- 角色 - 按职责对用户分组,并向组中已通过身份验证的成员授予资源权限。
- 配置文件 - 为已通过身份验证的用户保留属性,并在您的应用程序中检索这些属性。
如何扩展WebContext.Current
WebContext用来获得和管理应用程序路径上下文,Gets the context that is registered as a lifetime object with the current application. 当你在RiaService创建了AuthenticationDomainService以后,添加该RiaLink,就可以通过WebContext.Current.User
,WebContext.Current.Authentication.
....等来很方便的获得和管理用户身份和角色的上下文信息。需要注意的是,在Silverlight的project中如果添加了对AuthenticationDomainService的RiaService的RiaLink引用,就会在Silverlight项目中Generated_Code自动生成WebContext本地代理类:partial class WebContext : WebContextBase。这样我们才可以在Silverlight项目中调用WebContext.Current.User
,WebContext.Current.Authentication.
....等来很方便的获得和管理用户身份和角色的上下文信息。每一个要用到WebContext的Project都会在Silverlight项目中Generated_Code自动生成WebContext本地代理类,这可能造成冲突。
扩展WebContext.Current,添加自定义的属性
如何扩展WebContext.Current,添加自定义的属性?例如我想获得WebContext.Current.MyCustomizedData,怎么做?
其实很简单。首先,WebContext不可继承,WebContext.Current是一个静态单例访问,也不可继承。所以我们要扩展WebContext添加自定义的属性可以直接写一个WebContext.partial.cs,像这样:
{
//这是一个自定义类,换成你的自定义类
public CustomizedData MyCustomizedData
{
get;
set;
}
//另外一个属性
public IEnumerable<string> AnotherFields
{
get;
set;
}
}
这样就添加了自定义属性,通过WebContext.Current.MyCustomizedData访问。