|
YOUR FEEDBACK
|
TOP MICROSOFT .NET LINKS .NET News Desk AJAX RIA Tutorial - Accessing the ASP.NET Authentication, Profile and Role Service in Silverlight
How to access the WCF application services from a directly from the Silverlight client
By: Brad Abrams
Sep. 11, 2008 05:00 PM
In ASP.NET 2.0, we introduced a very powerful set of application services in ASP.NET (Membership, Roles and profile). In 3.5 we created a client library for accessing them from Ajax and .NET Clients and exposed them via WCF web services. For more information on the base level ASP.NET appservices that this walk through is based on, please see Stefan Schackow's excellent book Professional ASP.NET 2.0 Security, Membership, and Role Management. In this tutorial I will walk you through how to access the WCF application services from a directly from the Silverlight client. This works super well if you have a site that is already using the ASP.NET application services and you just need to access them from a Silverlight client. (Special thanks to Helen for a good chunk of this implantation) Here is what I plan to show: 1. Login\Logout
You can download the completed sample solution
We will need both the client side Silverlight project and the ASP.NET serverside project.
Let's configure our system with the test users. To do this we will use the ASP.NET Configuration Manager. In VS, under the Website menu, select "ASP.NET Configuration". Use this application to add a couple of users. I created two employees:
To expose the ASP.NET Authentication system, let's add a new WCF service. Because we are just going to point this at the default one that ships with ASP.NET, we don't need any code behind, so the easiest thing to do is to add a new Text File. In the ASP.NET website, Add New Item, select Text File and call it "AuthenticationService.svc"
Add this one line as the contents of the file. This wires it up to the implementation that ships as part of ASP.NET. <%@ ServiceHost Language="C#" Now in Web.config, we need to add the WCF magic to turn the service on. <system.serviceModel> <services> <!-- this enables the Now, still in Web.config, we need to enable forms authentication. Under the <system.web> change the authentication mode from "Windows" to "Forms". <authentication mode="Forms" /> One last change to web.config, we need to enable authentication to be exposed via the web service.This is done by adding a System.Web.Extensions section. <system.web.extensions> <scripting> <webServices> <authenticationService enabled=
Now, to consume this authentication service in Silverlight, let's open the page.xaml file and add some initial UI. Just buttons to log "employee" and "manager" in and a textblock to show some status. <Grid x:Name="LayoutRoot"
Right click on the Silverlight project and select Add Service Reference
Click Discover and set the namespace to "AuthenticationService"
If you get an error at this point, it is likely something wrong with your AuthenticationService.svc or the web config, go back and double check those. Now, let's write a little code to call that service to log us in. First add the right using statement using ApplicationServicesDemo.AuthenticationServices;
Then, in employeeLogIn_Click method write the code to call the service to log the employee in. For now, we will hard code the name in password, but by the end we will be prompting the user to get this data. First we create a the web services client class, then we call the login method asynchronously. Remember all network calls in Silverlight are async, otherwise we'd lock up the whole browser. Finally we sign up for the callback. private void employeeLogIn_Click In the callback, for now, let's just set our status. void client_LoginCompleted } Run it! You should see a good status. Try changing the password and ID, and see the status change to false. It is working.
Now do the same thing for manager and you are set! private void managerLogIn_Click Next Page - Part 2: Save Personalization Settings YOUR FEEDBACK
MICROSOFT .NET LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
|||||||||||||||||||||||||||||||||||||