您的位置:首页 > 其它

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

2010-06-05 15:12 423 查看
如何:在RIA Services中允许进行身份验证
WCF RIA Services中的身份验证是建立在ASP.NET验证框架之上的。
本章节展示如后在我们的应用程序中通过RIA Services来允许用户身份验证。我们必须在服务端和客户端添加代码,来使身份验证可行。这个验证对客户端就如同一个服务。我们可以通过对域操作应用 RequiresAuthenticationAttribute属性,来保证只有验证用户可以访问域操作。

配置服务端项目

1. 在服务端项目中,打开Web.config文件。
2. 在元素中,添加元素。
3. 设置mode属性为我们想在项目中使用的验证模式。下面的代码展示了mode属性设置为Forms的元素。当然,我们的Web.config文件可能包含其他元素。

1 <system.web>
2 <authentication mode="Forms"></authentication>
3 </system.web>

4. 保存Web.config文件。
5. 在资源管理器中,右键点击服务端项目,选择"添加->新项"。出现"添加新项"对话框。
6. 选择Authentication Domain Service模板,并制定一个名字。

  代码

1 private void LoadReports()
2 {
3 if (WebContext.Current.User.IsAuthenticated)
4 {
5 numberOfRows = WebContext.Current.User.DefaultRows;
6 WebContext.Current.User.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(User_PropertyChanged);
7 LoadRestrictedReports();
8 }
9 else
10 {
11 CustomersGrid.Visibility = System.Windows.Visibility.Collapsed;
12 SalesOrdersGrid.Visibility = System.Windows.Visibility.Collapsed;
13 }
14 LoadOperation<product> loadProducts = context.Load(context.GetProductsQuery().Take(numberOfRows));
15 ProductsGrid.ItemsSource = loadProducts.Entities;
16 }

8. 如果想让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/5462238.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: