Share via


HOW TO:啟用 RIA Services 中的角色

本主題示範如何在先前已經啟用驗證的情況下,啟用 WCF RIA Services 方案中的角色。您僅能在使用者經過驗證之後擷取使用者的角色。若要設定方案進行驗證,請參閱HOW TO:啟用 RIA Services 中的驗證。您要透過將 RequiresRoleAttribute 屬性套用至網域作業的方法,將網域作業的存取權限制為角色的成員。

角色用於指定哪個驗證使用者的群組可以存取特定資源。RIA Services 中的角色會建置在 ASP.NET 中的角色之上。如需角色的詳細資訊,請參閱認識角色管理

若要設定伺服器專案

  1. 在伺服器專案中,開啟 Web.config 檔案。

  2. <system.web> 區段中加入 <roleManager> 項目來啟用管理員角色。

    下列範例示範如何啟用管理員角色。

    <system.web>
      <authentication mode="Forms"></authentication>
      <roleManager enabled="true"></roleManager>
    </system.web>
    
  3. 在成員資格資料庫中,建立所需的角色,並在需要時,將使用者指派給角色。

    如需詳細資訊,請參閱認識角色管理。如需建立角色的範例,請參閱逐步解說:搭配 Silverlight 商務應用程式使用驗證服務逐步解說:搭配 Silverlight 瀏覽應用程式使用驗證服務

  4. 若要將網域作業的存取權限制為僅指定之角色的成員,請將 RequiresRoleAttribute 屬性套用至網域作業。

    下列範例會指定只有 Managers 角色的成員可以存取網域作業。

    <RequiresRole("Managers")> _
    Public Function GetCustomers() As IQueryable(Of Customer)
        Return Me.ObjectContext.Customers
    End Function
    
    [RequiresRole("Managers")]
    public IQueryable<Customer> GetCustomers()
    {
        return this.ObjectContext.Customers;
    }
    

若要存取用戶端專案中的角色

  1. 若要檢查使用者是否屬於必要角色,存取 Roles 屬性或呼叫 WebContext.Current.User 物件上 IsInRole 方法。

    下列範例會檢查使用者是否屬於名稱為 Managers 的角色,然後再呼叫網域作業。

    Private Sub LoadRestrictedReports()
        Dim loadSales = context.Load(context.GetSalesOrderHeadersQuery().Take(numberOfRows))
        SalesOrdersGrid.ItemsSource = loadSales.Entities
        SalesOrdersGrid.Visibility = System.Windows.Visibility.Visible
    
        If (WebContext.Current.User.IsInRole("Managers")) Then
            Dim loadCustomers = context.Load(context.GetCustomersQuery().Take(numberOfRows))
            CustomersGrid.ItemsSource = loadCustomers.Entities
            CustomersGrid.Visibility = System.Windows.Visibility.Visible
        Else
            CustomersGrid.Visibility = System.Windows.Visibility.Collapsed
        End If
    End Sub
    
    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 中使用,請將目前的 WebContext 執行個體加入至 Application.Startup 事件中的應用程式資源,然後再建立根 Visual。

    下列範例示範如何加入 WebContext 執行個體做為應用程式資源。

    Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
        Me.Resources.Add("WebContext", WebContext.Current)
        Me.RootVisual = New MainPage()
    End Sub
    
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        this.Resources.Add("WebContext", WebContext.Current);
        this.RootVisual = new MainPage();
    }
    

另請參閱

工作

逐步解說:搭配 Silverlight 瀏覽應用程式使用驗證服務
逐步解說:搭配 Silverlight 商務應用程式使用驗證服務