zoukankan      html  css  js  c++  java
  • Silverlight WCF RIA服务(三十一)身份验证、角色、个性化 2

    如何:在RIA Services中允许进行身份验证 WCF RIA Services中的身份验证是建立在ASP.NET验证框架之上的。 本章节展示如后在我们的应用程序中通过RIA Services来允许用户身份验证。我们必须在服务端和客户端添加代码,来使身份验证可行。这个验证对客户端就如同一个服务
      

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

    配置服务端项目

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

    1
    2
    3
    4
    5
    <SYSTEM.WEB>
      <AUTHENTICATION mode="Forms"></AUTHENTICATION>
    </SYSTEM.WEB>


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

    7. 点击"添加"。
    8. 如果想只有验证用户可以访问域操作,对域操作应用RequiresAuthenticationAttribute属性。下面代码展示了只有验证用户才可以访问GetSalesOrderHeaders方法。
     

    1
    2
    3
    4
    5
    6
    7
    [RequiresAuthentication()]
    public IQueryable<SALESORDERHEADER> GetSalesOrderHeaders()
    {
        return this.ObjectContext.SalesOrderHeaders;
    }


    9. 生成解决方案。

    在客户端配置身份验证服务

    1. 在客户端项目中,打开App.xaml文件。
    2. 在application lifetime 对象集合中添加当前的WebContext对象。下面的app前缀引用包含生成的WebContext对象的命名空间。通常,这个WebContext用的是客户端项目的基命名空间。
     

    1
    2
    3
    4
    5
    6
    <APPLICATION.APPLICATIONLIFETIMEOBJECTS>
      <APP:WEBCONTEXT>
      </APP:WEBCONTEXT>
    </APPLICATION.APPLICATIONLIFETIMEOBJECTS>


    3. 在WebContext元素内,添加验证模式。
    我们可以添加WindowsAuthentication或者FormsAuthentication。然而,我们添加的类型应该与服务端的验证模式一致。下面展示了如何添加FormsAuthentication。
     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <?XML:NAMESPACE PREFIX = [default] http://schemas.microsoft.com/winfx/2006/xaml/presentation NS = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" /><Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    </Application>             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:app="clr-namespace:ExampleApplication"
                 xmlns:appsvc="clr-namespace:System.Windows.Ria.ApplicationServices;assembly=System.Windows.Ria"
                 x:Class="ExampleApplication.App">
        <Application.Resources>
        </Application.Resources>
        <Application.ApplicationLifetimeObjects>
            <APP:WEBCONTEXT>
                <APP:WEBCONTEXT.AUTHENTICATION>
                    <APPSVC:FORMSAUTHENTICATION></APPSVC:FORMSAUTHENTICATION>
                </APP:WEBCONTEXT.AUTHENTICATION>
            </APP:WEBCONTEXT>
        </Application.ApplicationLifetimeObjects>


    4. 如果必要的话,在客户端添加一个页面来收集用户凭证。
    5. 在登录页面的后台代码文件中,调用Login方法来登录用户。下面的例子展示了如何从登录按钮的事件处理中调用Login方法。包含了一个回调函数来对登录操作的结果做反应。
     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    private void LoginButton_Click(object sender, RoutedEventArgs e)
    {
        LoginParameters lp = new LoginParameters(UserName.Text, Password.Password);
        WebContext.Current.Authentication.Login(lp, this.LoginOperation_Completed, null);
        LoginButton.IsEnabled = false;
        LoginResult.Text = "";
    }
     
    private void LoginOperation_Completed(LoginOperation lo)
    {
        if (lo.HasError)
        {
            LoginResult.Text = lo.Error.Message;
            LoginResult.Visibility = System.Windows.Visibility.Visible;
            lo.MarkErrorAsHandled();
        }
        else if (lo.LoginSuccess == false)
        {
            LoginResult.Text = "Login failed. Please check user name and password.";
            LoginResult.Visibility = System.Windows.Visibility.Visible;
        }
        else if (lo.LoginSuccess == true)
        {
            SetControlVisibility(true);
        }
        LoginButton.IsEnabled = true;
    }


    6. 调用Logout方法来登出用户。
     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    private void LogoutButton_Click(object sender, RoutedEventArgs e)
    {
        WebContext.Current.Authentication.Logout(this.LogoutOperation_Completed, null);
    }
     
    private void LogoutOperation_Completed(LogoutOperation lo)
    {
     
        if (!lo.HasError)
        {
            SetControlVisibility(false);
        }
        else
        {
            ErrorWindow ew = new ErrorWindow("Logout failed.""Please try logging out again.");
            ew.Show();
            lo.MarkErrorAsHandled();
        }
    }


    7. 要检测是否用户已登录,检索生成的User实体的IsAuthentication属性。下面的代码展示了在检索个性化信息和调用域操作之前,先检测是否当前用户已经验证了。
     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    private void LoadReports()
    {
        if (WebContext.Current.User.IsAuthenticated)
        {
            numberOfRows = WebContext.Current.User.DefaultRows;
            WebContext.Current.User.PropertyChanged += newSystem.ComponentModel.PropertyChangedEventHandler(User_PropertyChanged);
            LoadRestrictedReports();
        }
        else
        {
            CustomersGrid.Visibility = System.Windows.Visibility.Collapsed;
            SalesOrdersGrid.Visibility = System.Windows.Visibility.Collapsed;
        }
     
        LoadOperation<?XML:NAMESPACE PREFIX = [default] http://schemas.microsoft.com/winfx/2006/xaml/presentation NS = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" /><Product> loadProducts = context.Load(context.GetProductsQuery().Take(numberOfRows));
        ProductsGrid.ItemsSource = loadProducts.Entities;
    }


    8. 如果想让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();
    }

     

    Powered By D&J (URL:http://www.cnblogs.com/Areas/)
  • 相关阅读:
    ssh-keygen的使用方法(无密码访问)
    ubuntu solute two different terminals cmd
    ubuntu 查看系统是32位还是64位
    pyplot 绘图与可视化
    python 正则表达式的处理
    python&pandas 与mysql 连接
    Ubuntu 11.10 H3C iNode 客户端安装
    Vijos1055(极大子矩阵)
    Vijos1055(极大子矩阵)
    luoguP2701 [USACO5.3]巨大的牛棚Big Barn(极大子矩阵)
  • 原文地址:https://www.cnblogs.com/Areas/p/2172196.html
Copyright © 2011-2022 走看看