Walkthrough: Using Authentication Service with Silverlight Business Application
[ This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date
Please see Release Notes or Changelog for a list of changes since WCF RIA Services ]
The Silverlight Business Application template creates a solution that automatically enables authentication (with Forms Authentication for the authentication mode), roles, and profiles. The solution includes data forms for logging in existing users and registering new users. You can use these features without writing any additional code. You can customize the solution by defining roles and profile properties.
In this walkthrough, you will learn how to use authentication, roles, and profiles in a Silverlight Business Application. You will restrict access to certain domain operations based on the user's credentials and customize the user interface based on user preferences. You will use the ASP.NET Web Site Administration Tool for managing roles and users in the site.
This and the other walkthroughs presented in the Open Ria Services documentation require several prerequisite programs, such as Visual Studio and the Silverlight Developer Runtime and SDK, be installed and configured properly, in addition to Open Ria Services and the Open Ria Services Toolkit. They also require installing and configuring SQL Server 2008 R2 Express with Advanced Services and installing the AdventureWorks OLTP and LT database.
Detailed instructions for the satisfaction of each of these prerequisites are provided by the topics within the Prerequisites for Open Ria Services node. Follow the instructions provided there before proceeding with this walkthrough to ensure that you encounter as few problems as possible when working through this Open Ria Services walkthroughs.
You can use the provided features in a Silverlight Business Application to quickly implement authentication. In the following section, you will use the ASP.NET Configuration tool to create a user and role, and then log in as that user. You will register a new user through the registration form that is provided in the Silverlight Business Application.
- 1.In Visual Studio, select File, New, and then Project.The New Project dialog box appears.
- 2.Select the Silverlight project type.
- 3.Select the Silverlight Business Application template and name the application ExampleBusinessApplication.
- 4.Click OK.Notice the project structure that is created. The Silverlight client project includes Silverlight pages in the Views folder. These pages enable logging in users and registering new users.
- 5.To open the ASP.NET Web Site Administration Tool, first select the server project (ExampleBusinessApplication.Web) in Solution Explorer. open the ASP.NET Configuration tool.
- 6.On the Project menu, select ASP.NET Configuration.If you do not see the ASP.NET Configuration option in the Project menu, it may be because you have selected the client project.
- 7.Select the Security tab in the ASP.NET Web Site Administration Tool.
- 8.In the Roles section, click the Create or Mange roles link.
- 9.Add a new role named Managers and select the Add Role button.
- 10.In the lower-right corner, click the Back button.
- 11.In the Users section, click the Create user link.
- 12.Create a new user with the following values and select the Managers role check box.User Name: CustomerManagerPassword: [email protected]E-mail: [email protected]Security Question: Favorite color?Security Answer: BlueManagers role: selected
- 13.Click the Create User button.
- 14.Close the ASP.NET Web Site Administration Tool.
- 15.Run the solution.The Home page for the application appears in a Web browser.
- 16.In the upper-right corner of the page, click the login link.A Login dialog box appears.
- 17.Enter CustomerManager for the user name and [email protected] for the password, and click OK button.You are now logged in as that user. Notice the text "Welcome CustomerManager" in the upper-right corner.
- 18.Click the logout link.You are no longer logged in as CustomerManager. In the following steps, you will create a new user through the registration form.
- 19.Click login link again.
- 20.In the Login dialog box, click the Register now link.The registration form is now displayed.
- 21.Fill out the registration form to create a new user account. Use the following values for the new user.Username: SalesUserFriendly name: SalesUserEmail: [email protected]Password: [email protected]Security Question: What was the color of your first car?Security Answer: Green
- 22.Click OK to create the new user.Notice that you are now logged in as SalesUser.
- 23.Close the browser.
- 24.Open the ASP.NET Web Site Administration Tool again, and click the Security tab.Notice that 2 users now exist in the site and 2 roles exist even though you have only created one role.
- 25.Click Create or Manage roles and notice the Managers role and the Registered Users.The Registered Users role was automatically created by the Business Application template.
- 26.For Registered Users, click the Manage link.Notice that the user named SalesUser that you added through the application is in the Registered Users role.
- 27.Close the ASP.NET Web Site Administration Tool.
You restrict access to a domain operation by applying either the RequiresAuthenticationAttribute attribute or the RequiresRoleAttribute attribute to the domain operation. Domain operations without an attribute are available to all users. Applying an attribute to domain operation does not prevent the user from calling the domain operation; however, users without the required credentials will receive an exception.
- 1.In Solution Explorer, right-click the App_Data folder in the server project, select Add and then Existing Item.
- 2.In the Add Existing Item dialog box, add the AdventureWorksLT sample database.
- 3.In the server project, add a new item and select the ADO.NET Entity Data Model template from the Data templates.
- 4.Name the model AdventureWorksModel.edmx and click Add.The Entity Data Model Wizard appears.
- 5.Select the Generate from database option and then click Next.
- 6.Select the AdventureWorksLT database and then click Next.
- 7.From the list of database objects, select the Customer, Product, and SalesOrderHeader tables, and then click Finish.The entity data model appears in the designer.
- 8.Build the solution.
- 9.In the server project, add a new item and select the Domain Service Class template from the Web templates.
- 10.Name the domain service AdventureWorksDomainService and then click Add.
- 11.In the Add New Domain Service Class dialog box, select the Customer, Product, and SalesOrderHeader entities.
- 12.Click OK to finish creating the domain service.
- 13.In the AdventureWorksDomainService class file, add the RequiresAuthenticationAttribute attribute to GetSalesOrderHeader method.<RequiresAuthentication()> _Public Function GetSalesOrderHeaders() As IQueryable(Of SalesOrderHeader)Return Me.ObjectContext.SalesOrderHeadersEnd Function[RequiresAuthentication()]public IQueryable<SalesOrderHeader> GetSalesOrderHeaders(){return this.ObjectContext.SalesOrderHeaders;}
- 14.Add the RequiresRoleAttribute attribute to the GetCustomers method, and set the name of the required role to "Managers".<RequiresRole("Managers")> _Public Function GetCustomers() As IQueryable(Of Customer)Return Me.ObjectContext.CustomersEnd Function[RequiresRole("Managers")]public IQueryable<Customer> GetCustomers(){return this.ObjectContext.Customers;}The GetProducts domain operation is available to any user, GetSalesOrderHeaders is available to authenticated users, and GetCustomers is available to only users in the Managers role.The following shows the complete domain service.<EnableClientAccess()> _Public Class AdventureWorksDomainServiceInherits LinqToEntitiesDomainService(Of AdventureWorksLT_DataEntities)<RequiresRole("Managers")> _Public Function GetCustomers() As IQueryable(Of Customer)Return Me.ObjectContext.CustomersEnd FunctionPublic Function GetProducts() As IQueryable(Of Product)Return Me.ObjectContext.ProductsEnd Function<RequiresAuthentication()> _Public Function GetSalesOrderHeaders() As IQueryable(Of SalesOrderHeader)Return Me.ObjectContext.SalesOrderHeadersEnd FunctionEnd Class[EnableClientAccess()]public class AdventureWorksDomainService : LinqToEntitiesDomainService<AdventureWorksLT_DataEntities>{[RequiresRole("Managers")]public IQueryable<Customer> GetCustomers(){return this.ObjectContext.Customers;}public IQueryable<Product> GetProducts(){return this.ObjectContext.Products;}[RequiresAuthentication()]public IQueryable<SalesOrderHeader> GetSalesOrderHeaders(){return this.ObjectContext.SalesOrderHeaders;}}
You define a profile property in the Web.config file. When you add the property to the User class on the server, the corresponding property is generated for the client project.
- 1.In the server project, open the Web.config file.
- 2.In the \ element, add a profile property named DefaultRows. The property will contain the user's preference for the number of rows to display.The following shows the profile section of the Web.config file.<profile><properties><add name="FriendlyName" /><add type="System.Int32" defaultValue="10" name="DefaultRows"/></properties></profile>
- 3.Save the Web.config file.
- 4.In the server project, expand the Models folder.
- 5.Open the User.cs or User.vb file, and add a property named DefaultRows.Imports OpenRiaServices.Server.AuthenticationImports System.Runtime.SerializationNamespace WebPartial Public Class UserInherits UserBasePublic Property FriendlyName As String