Skip to main content

Microsoft Silverlight

Tim Heuer Written By:
Tim Heuer
Microsoft

Part 1, Silverlight Authentication & Role Validation

0 0

Summary

Welcome to the first part of building the AdventureWorks Ops business application.  In this first part, we look at integrating authentication and authorization services provided by ASP.NET Application Services.  Our goal is to leverage the included services that ASP.NET provides for the means of user authentication and role validation.  ASP.NET provides these as a part of the runtime and also gives us a method of exposing these core features as Windows Communication Foundation (WCF) services.  This video will walk you through the steps required and demonstrate how we set up logging in various users via Silverlight as well as using their role information to determine their application capabilities.

Topics Covered


Setting up the Services

To set up the services, we first have to have a web application to host them.  Once we have that, we’ll enable the services as WCF endpoints.  Let’s look at the Authentication service first (the process for Roles will be the same).  First, in Visual Studio 2008, add a new item to the web project.  Choose a Text File so that we don’t get all the stuff from the other WCF item templates (WCF Service and Silverlight-enabled WCF Service) initially…we’ll add our own information—you can call the file Auth.svc.

In the new text file, you’ll add one line as displayed here:

CS

1: <%@ ServiceHost Language="C#" Service="System.Web.ApplicationServices.AuthenticationService" %>

That’s it for the .svc file…we don’t need anything else in there.  By indicating the Service attribute pointing to the class for our AuthenticationService, there is nothing more that this particular file needs as content…it is serving as an endpoint now.

Enabling the WCF contract

The next step is to provide the service information in the web.config of our ASP.NET application.  We will need the system.serviceModel node there and add the behavior, bindings, services and ASP.NET compatibility child nodes to that.  For our authentication service it looks like this:

CS

1: <system.serviceModel> 2: <behaviors> 3: <serviceBehaviors> 4: <behavior name="AppServicesBehavior"> 5: <serviceMetadata httpGetEnabled="true"/> 6: </behavior> 7: </serviceBehaviors> 8: </behaviors> 9: 10: <bindings> 11: <basicHttpBinding> 12: <binding name="userHttp"> 13:          <!-- you would actually want to use a real security mode in a production env --> 14: <security mode="None"/> 15: </binding> 16: </basicHttpBinding> 17: </bindings> 18: 19: <services> 20: <service name="System.Web.ApplicationServices.AuthenticationService" behaviorConfiguration="AppServicesBehavior"> 21: <endpoint contract="System.Web.ApplicationServices.AuthenticationService" binding="basicHttpBinding" bindingConfiguration="userHttp" bindingNamespace="http://asp.net/ApplicationServices/v200"/> 22: </service> 23: </services> 24: 25: <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 26: </system.serviceModel>

By having this definition we’ve now completed the WCF configuration aspects of the service.  Notice the basicHttpBinding type as that is the type that is currently supported in Silverlight.  We now need to tell ASP.NET to enable these application services as scriptable services.  We do this in the system.web.extensions node of the web.config and add this information:

CS

1: <system.web.extensions> 2: <scripting> 3: <webServices> 4: <authenticationService enabled="true" requireSSL="false"/> 5: </webServices> 6: </scripting> 7: </system.web.extensions>

Calling the Services

This now enables the service endpoint (Auth.svc file) to be called as a service and scriptable.  Now when we call them we will follow the WCF asynchronous pattern for Silverlight.  First in our Silverlight project, we will add Service Reference to our project and point to the Auth.svc endpoint that we’ve created.

Now in our LoginButton Click event handler we will call this code.  We will instantiate a new AuthenticationServiceClient, which is an object that was generated for us as proxy code when we added the service reference in Visual Studio.  We’ll add an event handler to the LoginCompleted event to process the result of our login attempt, and then we’ll call the Login command, which is actually LoginAsync.  In our completed event handler, we’ll look at the result and act upon it.  Our code will look somewhat like this:

CS

1: void LoginClicked(object sender, RoutedEventArgs e) 2: { 3: LoginProgress.Visibility = Visibility.Visible; 4: AuthenticationServiceClient auth = new AuthenticationServiceClient(); 5: auth.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(LoginCompleted); 6: auth.LoginAsync(UserName.Text, UserPassword.Password, string.Empty, true, UserName.Text); 7: } 8: 9: void LoginCompleted(object sender, LoginCompletedEventArgs e) 10: { 11: LoginProgress.Visibility = Visibility.Collapsed; 12: if (e.Error != null) 13: { 14:    // TODO: Show error message 15: } 16: else 17: { 18: if (!e.Result) 19: { 20:     // TODO: login failed, show error 21: } 22: else 23: { 24:     // TODO: Login succeeded, display dashboard 25: } 26: } 27: }

Tying it together

The video demonstrates tying all these together as well as enabling the RoleService and incorporating that into our application to determine functionality in our user interface.  Take a look at the code for this first part to see how we were able to leverage the included features of ASP.NET Application Services in our Silverlight application.  In the next part, we’ll use that same authorization information to secure our data service layer and implement data binding and editing in our Employee application.

Leave a Comment Comments (9) RSS Feed


jayme...

Member

Member

3 Points

#1 September 16, 2009 5:51 PM

This is an excellent application. I m looking for its next parts. When will it be available?


craigpj

Member

Member

1 Points

#2 September 17, 2009 10:59 AM

Can you please provide a link to part two of this great tutorial.


james...

Member

Member

1 Points

#3 September 23, 2009 12:59 PM

Have been a legacy database programmer for many years, but as I am a total newbie to Visual Studio 2008, ASP.NET, Silverlight, LINQ, etc this whole thing is a huge learning curve. Your tutorial looks like its going to be exactly what is needed by those in a similar situation to get started - but please could you let us have the next part asap, or let us know when it will be ready?

Thanks Tim


hncdyjh

Member

Member

24 Points

#4 October 14, 2009 3:37 AM

What about a polling duplex service?


micha...

Member

Member

1 Points

#5 October 19, 2009 4:26 AM

This is fantastic. I watched it through and started over ready to code... The crash and burn was at the green light.

You see... to install the Db, you need SQL Server to have Full Text Searching and some SQL Daemon or another. So try to install SQL Server Express with those features. Well, you need C# Visual Studio with SP1 to do that... which you cannot get for Visual Studio Express.

Bummer... Still, I'm going to try to impliment it, just without the Adventure Gear Db.

Nice tutorial. I want Silverlight!

T'anks, Miguelito


silve...

Member

Member

11 Points

#6 October 19, 2009 9:08 PM

Hi,

Is the binding namespace correct:

bindingNamespace=&quot;http://asp.net/ApplicationServices/v200&quot;/&gt;

As im getting some funky error and when trying to access that link I got a file not found error...

Error:

Service 'System.Web.ApplicationServices.AuthenticationService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

Thanks


sbrow...

Member

Member

3 Points

#7 October 25, 2009 10:00 PM

I'm having some trouble with this implementation. Almost everything works. I can do the login authorization without any problems, and I've implemented the SSL encryption layer.

However, I can't seem to make a call to a secured function. For example...

[PrincipalPermission(SecurityAction.Demand, Role=&quot;sysadmin&quot;)]

public string TestPermission()

{

return HttpContext.Current.User.IsInRole(&quot;sysadmin&quot;).ToString();

}

If I login as a user in the sysadmin role and call this function without the PrinciplePermission decoration, it all works fine. It even returns &quot;true&quot;, which demonstrates that the user context is correct.

But when I include the decoration, I get a &quot;not found&quot; error back from the server. That usually means that there's something wrong with the soap message.

Any ideas? Adding the decoration is literally the only change needed to cause the error.

Thanks,

Steve


paulo...

Member

Member

4 Points

#8 November 03, 2009 12:02 PM

Hi,

I'm using Role service as we can see on this example code

Every thing work's well when i try it on development webserver. When i deploy Auth and Role services to a remote IIS, i invoke GetRolesForCurrentUser and always received a empty collection of roles.

Any special configuration about RoleService on IIS ??

Pleaseeeee I'm completely lost.

Thanks in advance!!!

Paulo


akora...

Member

Member

1 Points

#9 November 12, 2009 8:53 PM

This is video is put together very well and great intro to what you can do with Silverlight. When will part 11 be available

Alex

  • 1

You must be logged in to leave a comment. Click here to log in.

Tim Heuer The Application Corner
with Tim Heuer

Microsoft Communities