现在的位置: 首页 > 综合 > 正文

[Silverlight入门系列]扩展WebContext.Current(Authentication,Roles,Profiles)

2013年01月17日 ⁄ 综合 ⁄ 共 1559字 ⁄ 字号 评论关闭

关于用户登录、用户角色和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.UserWebContext.Current.Authentication.....等来很方便的获得和管理用户身份和角色的上下文信息。需要注意的是,在Silverlight的project中如果添加了对AuthenticationDomainService的RiaService的RiaLink引用,就会在Silverlight项目中Generated_Code自动生成WebContext本地代理类:partial class WebContext : WebContextBase。这样我们才可以在Silverlight项目中调用WebContext.Current.UserWebContext.Current.Authentication.....等来很方便的获得和管理用户身份和角色的上下文信息。每一个要用到WebContext的Project都会在Silverlight项目中Generated_Code自动生成WebContext本地代理类,这可能造成冲突。

扩展WebContext.Current,添加自定义的属性

如何扩展WebContext.Current,添加自定义的属性?例如我想获得WebContext.Current.MyCustomizedData,怎么做?

其实很简单。首先,WebContext不可继承,WebContext.Current是一个静态单例访问,也不可继承。所以我们要扩展WebContext添加自定义的属性可以直接写一个WebContext.partial.cs,像这样:

public partial class WebContext
{
    
//这是一个自定义类,换成你的自定义类
    public CustomizedData MyCustomizedData
    {
        
get;
        
set;
    }

    //另外一个属性
    public IEnumerable<string> AnotherFields
    {
        
get;
        
set;
    }
}

这样就添加了自定义属性,通过WebContext.Current.MyCustomizedData访问。

 

抱歉!评论已关闭.