神仙?妖怪?谢谢!

Just do it...

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

如何:在RIA Services中允许角色功能
使用角色,我们可以指定哪个验证用户组可以访问某些资源。 WCF RIA Services中的角色功能是建立在ASP.NET的角色功能上的。
我们只有在用户已经被验证后,才能检索用户的角色信息。通过在域操作中的方法上使用RequireRoleAttribute属性,我们就可以限制角色中的成员对域操作的访问。

 

配置服务端项目
   1.  在服务端项目中,打开Web.config文件。
   2.  在段中,添加元素。

1 <system.web>  
2   <authentication mode="Forms"></authentication>  
3   <rolemanager enabled="true"></rolemanager>  
4 </system.web>

 

   3.  在成员数据库中,创建所需的角色并赋予用户所需的角色。更多详情,可看后面的章节。
   4.  要确保只有指定角色中的成员才能访问域操作,我们需要对域操作应用RequireRoleAttribute属性。

1 [RequiresRole("Managers")]   
2 public IQueryable<customer> GetCustomers()   
3 {   
4     return this.ObjectContext.Customers;   
5 }

 

 

在客户端使用角色
   1.  要检测是否用户属于要求的角色,使用Roles属性或调用WebContext.Current.User对象的IsInRole方法。下面示例了在调用域操作之前,检测是否用户属于Managers的角色。

代码
 1 private void LoadRestrictedReports()   
 2 {   
 3     LoadOperation<salesorderheader> loadSales = context.Load(context.GetSalesOrderHeadersQuery().Take(numberOfRows));   
 4     SalesOrdersGrid.ItemsSource = loadSales.Entities;   
 5     SalesOrdersGrid.Visibility = System.Windows.Visibility.Visible;   
 6     if (WebContext.Current.User.IsInRole("Managers"))   
 7     {   
 8         LoadOperation<customer> loadCustomers = context.Load(context.GetCustomersQuery().Take(numberOfRows));   
 9         CustomersGrid.ItemsSource = loadCustomers.Entities;   
10         CustomersGrid.Visibility = System.Windows.Visibility.Visible;   
11     }   
12     else  
13     {   
14         CustomersGrid.Visibility = System.Windows.Visibility.Collapsed;   
15     }   
16 }

 

   2.  如果想让WebContext对象在XAML中可用,那么在创建RootVisual之前,在Application.Startup事件中把当前 WebContext实例添加到应用程序资源中。

1 private void Application_Startup(object sender, StartupEventArgs e)   
2 {   
3     this.Resources.Add("WebContext", WebContext.Current);   
4     this.RootVisual = new MainPage();   
5 }

 

 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/blackant2/archive/2010/04/08/5462403.aspx

posted on 2010-06-05 15:15  E.Trock  阅读(631)  评论(0编辑  收藏  举报