[Silverlight entry series] WCF Ria service authentication user role Authentication

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.