How to: Enable Authentication in RIA Services
This topic demonstrates how to enable user authentication in your application by using WCF RIA Services. It shows the code that you must add to both the server project and the client project to make authentication available as a service to the client application. You can restrict access to a domain operation to only authenticated users by applying the RequiresAuthenticationAttribute attribute to the domain operation.
Authentication in WCF RIA Services builds upon the authentication framework in ASP.NET. For more information about ASP.NET authentication, see Introduction to Membership.
To configure the server project
In the server project, open the Web.config file.
<system.web>element, add an
modeproperty to the authentication mode that you will use in the project.
The following code shows the
Forms. Set the
Windowsto use Windows Authentication. Your Web.config file will contain other elements.
<system.web> <authentication mode="Forms"></authentication> </system.web>
Save the Web.config file.
In Solution Explorer, right-click the server project, select Add and then New Item.
The Add New Item dialog box appears.
Select the Authentication Domain Service template and specify a name for the service.
To restrict access to a domain operation to only authenticated users, apply the RequiresAuthenticationAttribute attribute to the domain operation.
The following example specifies that only authenticated users can access the
Build the solution.
To configure the authentication service on the client project
In the client project, open the code-behind file for the App.xaml file (App.xaml.cs or App.xaml.vb).
In the constructor, create an instance of the WebContext class.
Set the Authentication property to the type of authentication that you configured in the server project, and add the WebContext instance to the ApplicationLifetimeObjects.
The following example shows how to set authentication to FormsAuthentication.
If you are using Windows Authentication or you want to load a user who has persisted credentials, call the LoadUser method before giving the user the option to log in.
The following example shows how to call the LoadUser method from the
If necessary, add a page to the client project for collecting user credentials.
In the code-behind file for the login page, call the Login method to login users.
The following example shows how to call the Login method from an event handler for a login button. A callback method is included to respond to the results of the login operation.
To logout users, call the Logout method.
The following example shows how to call the Logout method from an event handler for a logout button. A callback method is included to respond to the results of the logout operation.
To check whether a user is authenticated, retrieve the IsAuthenticated property on the generated User entity.
The following example checks if the current user is authenticated before retrieving a profile property and calling a domain operation.
If you want to make the WebContext object available in XAML, add the current WebContext instance to the application resources in the Application.Startup event before creating the root visual.
The following example shows how to add the WebContext instance as an application resource.