如何:在RIA Services中允许角色功能
使用角色,我们可以指定哪个验证用户组可以访问某些资源。WCF RIA Services中的角色功能是建立在ASP.NET的角色功能上的。
我们只有在用户已经被验证后,才能检索用户的角色信息。通过在域 *** 作中的方法上使用RequireRoleAttribute属性,我们就可以限制角色中的成员对域 *** 作的访问。
配置服务端项目
1. 在服务端项目中,打开Web.config文件。
2. 在段中,添加元素。
1 2 3 4 5 6 | < SYstem.WEB > < AUTHENTICATION mode = "Forms" ></ AUTHENTICATION > < RolEMANAGER enabled = "true" ></ RolEMANAGER > </ SYstem.WEB > |
3. 在成员数据库中,创建所需的角色并赋予用户所需的角色。更多详情,可看后面的章节。
4. 要确保只有指定角色中的成员才能访问域 *** 作,我们需要对域 *** 作应用RequireRoleAttribute属性。
1 2 3 4 5 6 7 | [RequiresRole( "Managers" )] public Iqueryable<CUSTOMER> GetCustomers() { return this .ObjectContext.Customers; } |
在客户端使用角色
1. 要检测是否用户属于要求的角色,使用Roles属性或调用WebContext.Current.User对象的IsInRole方法。下面示例了在调用域 *** 作之前,检测是否用户属于Managers的角色。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | private voID LoadRestrictedReports() { LoadOperation<SALESORDERheader> loadSales = context.Load(context.GetSalesOrderheadersquery().Take(numberOfRows)); SalesOrdersGrID.ItemsSource = loadSales.EntitIEs; SalesOrdersGrID.Visibility = System.windows.Visibility.Visible; if (WebContext.Current.User.IsInRole( "Managers" )) { LoadOperation<CUSTOMER> loadCustomers = context.Load(context.GetCustomersquery().Take(numberOfRows)); CustomersGrID.ItemsSource = loadCustomers.EntitIEs; CustomersGrID.Visibility = System.windows.Visibility.Visible; } else { CustomersGrID.Visibility = System.windows.Visibility.Collapsed; } } |
2. 如果想让WebContext对象在XAML中可用,那么在创建RootVisual之前,在Application.Startup事件中把当前WebContext实例添加到应用程序资源中。
1 2 3 4 5 6 7 | private voID Application_Startup( object sender,StartupEventArgs e) { this .Resources.Add( "WebContext" ,WebContext.Current); this .RootVisual = new MainPage(); } |
以上是内存溢出为你收集整理的Silverlight WCF RIA服务(三十二)身份验证、角色、个性化 3全部内容,希望文章能够帮你解决Silverlight WCF RIA服务(三十二)身份验证、角色、个性化 3所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)