WCF RIA 服务 (三十二)-- 身份验证、角色、个性化 3

如何:在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属性。
[c-sharp] view plain copy print ?
  1. [RequiresRole("Managers")] 
  2. public IQueryable<customer> GetCustomers() 
  3.     return this.ObjectContext.Customers; 



在 客户端使用角色
1. 要检测是否用户属于要求的角色,使用Roles属性或调用WebContext.Current.User对象的IsInRole方法。下面示例了在调用 域操作之前,检测是否用户属于Managers的角色。
[c-sharp] view plain copy print ?
  1. private void LoadRestrictedReports() 
  2.     LoadOperation<salesorderheader> loadSales = context.Load(context.GetSalesOrderHeadersQuery().Take(numberOfRows)); 
  3.     SalesOrdersGrid.ItemsSource = loadSales.Entities; 
  4.     SalesOrdersGrid.Visibility = System.Windows.Visibility.Visible; 
  5.     if (WebContext.Current.User.IsInRole("Managers")) 
  6.     { 
  7.         LoadOperation<customer> loadCustomers = context.Load(context.GetCustomersQuery().Take(numberOfRows)); 
  8.         CustomersGrid.ItemsSource = loadCustomers.Entities; 
  9.         CustomersGrid.Visibility = System.Windows.Visibility.Visible; 
  10.     } 
  11.     else 
  12.     { 
  13.         CustomersGrid.Visibility = System.Windows.Visibility.Collapsed; 
  14.     } 


2. 如果想让WebContext对象在XAML中可用,那么在创建RootVisual之前,在Application.Startup事件中把当前WebContext实例添加到应用程序资源中。
[c-sharp] view plain copy print ?
  1. private void Application_Startup(object sender, StartupEventArgs e) 
  2.     this.Resources.Add("WebContext", WebContext.Current); 
  3.     this.RootVisual = new MainPage(); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值