First
Add a new item to the WCF Ria Service Project:
Domain service class
Then add the authentication and access control attributes to it:
[Enableclientaccess ()]
Public Class Adventureworksdomainservice: linqtoentitiesdomainservice < Adventureworkslt_dataentities >
{
// Required role: permission of the manager
[Requiresrole ( " Managers " )]
Public Iqueryable < Customer > Getcustomers ()
{
Return This . Objectcontext. customers;
}
PublicIqueryable<Product>Getproducts ()
{
Return This. Objectcontext. Products;
}
//Authentication Required
[Requiresauthentication ()]
PublicIqueryable<Salesorderheader>Getsalesorderheaders ()
{
Return This. Objectcontext. salesorderheaders;
}
}
At the Silverlight front-end, we first drag two grids at the front-end: datagrid1 and datagrid2.
BackgroundCode:
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. net;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. documents;
Using System. Windows. input;
Using System. Windows. Media;
Using System. Windows. Media. animation;
Using System. Windows. shapes;
Using System. Windows. Navigation;
Using Businessapplication1.web;
Using System. Windows. Data;
Using System. servicemodel. domainservices. Client. applicationservices;
namespace businessapplication1.views
{< br> Public partial class page1: page
{< br> domainservice1 S = New domainservice1 ();
PublicPage1 ()
{
Initializecomponent ();
Webcontext. Current. Authentication. loggedin + = New System. eventhandler < Authenticationeventargs > (Authentication_loggedin );
Webcontext. Current. Authentication. loggedout + = New System. eventhandler < Authenticationeventargs > (Authentication_loggedout );
Loadrestrictedcontents ();
}
void authentication_loggedin ( Object sender, authenticationeventargs e)
{< br> loadrestrictedcontents ();
}
VoidAuthentication_loggedout (ObjectSender, authenticationeventargs E)
{
Datagrid1.visibility=System. Windows. Visibility. collapsed;
Datagrid2.visibility=System. Windows. Visibility. collapsed;
}
Private Void Loadrestrictedcontents ()
{
// Pagedcollectionview itemlistview = new pagedcollectionview (S. companies );
If (Webcontext. Current. User. isinrole ( " Managers " ))
{
S. Load (S. getcompaniesquery ());
Datagrid1.itemssource = S. Companies;
Datagrid1.visibility = System. Windows. Visibility. visible;
}
Else
{
Datagrid1.visibility = System. Windows. Visibility. collapsed;
}
If(Webcontext. Current. User. isauthenticated)
{
S. Load (S. gethardwareproducttypesquery ());
datagrid2.itemssource = S. hardwareproducttypes;
datagrid2.visibility = system. windows. visibility. visible;
}
Else
{
Datagrid2.visibility=System. Windows. Visibility. collapsed;
}
//Datapager1.source = itemlistview;
}
//Executes when the user navigates to this page.
Protected Override VoidOnnavigatedto (navigationeventargs E)
{
}
}
}
When calling the two methods of the WCF Ria service, some permission checks are added. If the conditions are met, the DataGrid is displayed; otherwise, the DataGrid is not displayed.
Complete example on Microsoft Official Website: http://msdn.microsoft.com/en-us/library/ee942449 (V = vs.91). aspx
Continue next time.