Authentication, Roles, and Profiles
[ 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 ]
You add the authentication domain service to your Open Ria Services solution when you want to verify a user's credentials, restrict access for certain operations, or retain properties for each user from your client project. In a traditional ASP.NET Web application, you can use the ASP.NET Membership framework to perform these functions. Open Ria Services builds upon the ASP.NET Membership framework by exposing the Membership framework to rich Internet clients through the authentication domain service. After adding an authentication domain service, you can enable the following functions:
  • Authentication - to verify a user's credentials and mark the user as logged in or logged out.
  • Roles - to group users by responsibilities and grant resource permissions to authenticated members of a group.
  • Profiles - to retain properties for authenticated users and retrieve those properties in your application.
This topic introduces how you use authentication, roles, and profiles in a Open Ria Services solution.

Authentication Domain Service

Open Ria Services provides the Authentication Domain Service template to facilitate accessing authentication, roles, and profiles on the presentation tier. To create an authentication domain service, you simply create a new item in the server project and select the Authentication Domain Service template when creating the item.
RIA_ServicesAddAuth
When you add an authentication domain service, the Open Ria Services framework automatically adds two classes to the server project. The class that represents the authentication service derives from the AuthenticationBase\ class. The class that represents the user derives from the UserBase class. The user class contains the profile properties for an authenticated user.
When you build the solution, Open Ria Services automatically generates a WebContext class in the client project. The WebContext class enables you to access the authentication domain service and the user in your client project. You use the Current property to retrieve the current instance of the WebContext. The WebContext class derives from WebContextBase.
For an example of how to add an authentication domain service to a Open Ria Services solution, see Walkthrough: Using Authentication Service with Silverlight Navigation Application.

Silverlight Business Application and Authentication

When you select the Silverlight Business Application template to create a solution, the solution automatically includes an authentication domain service and controls to manage logging in and registering users. By default, the solution uses Forms authentication, but you can easily configure it for Windows authentication. Roles are enabled and one profile property is defined. For an example of the authentication features that are included by default in a Silverlight Business Application and how you change the configuration from Forms authentication to Windows authentication, see Walkthrough: Using the Silverlight Business Application Template. For an example of building upon the default features in a Silverlight Business Application, see Walkthrough: Using Authentication Service with Silverlight Business Application.
The following illustration shows the registration window, which is one of the default features included in the Silverlight Business Application.
Register dialog box

Authentication

Open Ria Services provides classes that enable you to easily implement Forms authentication or Windows authentication in your solution. To use authentication in your Open Ria Services solution, you must configure the server project and the client project for authentication. For more information, see How to: Enable Authentication in Open Ria Services.
After configuring the server and client projects, you asynchronously log in users from your Silverlight application by calling the Login method on the WebContext object. When using Windows authentication or when retrieving a user with persisted credentials, you do not have to call the Login method. Instead, you call the LoadUser method to retrieve the user that is authenticated through Windows authentication or to load a user with persisted credentials.
The following methods and properties are typically used in the client project when you have implemented authentication.
Member
Code to use
Task
​Authentication​
WebContext.Current.Authentication
To access the authentication service.
​User​
WebContext.Current.User
To access the object that contains the state of the user.
WebContext.Current.Authentication.Login(string, string)
-or-
WebContext.Current.Authentication.Login(LoginParameters)
-or-
WebContext.Current.Authentication.Login(LoginParameters, LoginOperation, object)
To asynchronously verify the user's credentials.
WebContext.Current.Authentication.Logout(boolean)
-or-
WebContext.Current.Authentication.Logout(LogoutOperation, object)
To asynchronously log out an authenticated user.
WebContext.Current.Authentication.LoadUser()
-or-
WebContext.Current.Authentication.LoadUser(LoadUserOperation, object)
To load an authenticated user, refresh user state, load persisted user authentication, or retrieve principal user object when used with Windows authentication.
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.
1
Private Sub LoginButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
2
Dim lp As LoginParameters = New LoginParameters(UserName.Text, Password.Password)
3
WebContext.Current.Authentication.Login(lp, AddressOf Me.LoginOperation_Completed, Nothing)
4
LoginButton.IsEnabled = False
5
LoginResult.Text = ""
6
End Sub
7
​
8
Private Sub LoginOperation_Completed(ByVal lo As LoginOperation)
9
If (lo.HasError) Then
10
LoginResult.Text = lo.Error.Message
11
LoginResult.Visibility = System.Windows.Visibility.Visible
12
lo.MarkErrorAsHandled()
13
ElseIf (lo.LoginSuccess = False) Then
14
LoginResult.Text = "Login failed. Please check user name and password."
15
LoginResult.Visibility = System.Windows.Visibility.Visible
16
ElseIf (lo.LoginSuccess = True) Then
17
SetControlVisibility(True)
18
End If
19
LoginButton.IsEnabled = True
20
End Sub
Copied!
1
private void LoginButton_Click(object sender, RoutedEventArgs e)
2
{
3
LoginParameters lp = new LoginParameters(UserName.Text, Password.Password);
4
WebContext.Current.Authentication.Login(lp, this.LoginOperation_Completed, null);
5
LoginButton.IsEnabled = false;
6
LoginResult.Text = "";
7
}
8
​
9
private void LoginOperation_Completed(LoginOperation lo)
10
{
11
if (lo.HasError)
12
{
13
LoginResult.Text = lo.Error.Message;
14
LoginResult.Visibility = System.Windows.Visibility.Visible;
15
lo.MarkErrorAsHandled();
16
}
17
else if (lo.LoginSuccess == false)
18
{
19
LoginResult.Text = "Login failed. Please check user name and password.";
20
LoginResult.Visibility = System.Windows.Visibility.Visible;
21
}
22
else if (lo.LoginSuccess == true)
23
{
24
SetControlVisibility(true);
25
}
26
LoginButton.IsEnabled = true;
27
}
Copied!

Roles

After you have implemented authentication, you can configure your solution to use roles. With roles, you can assign users to groups. Then, you can specify that a particular domain operation is only available to members of that role. You restrict access to a domain operation by applying the RequiresRoleAttribute to the domain operation. For more information, see How to: Enable Roles in Open Ria Services.
The following methods and properties are typically used when you have implemented roles.
Member
Code to use
Task
​Roles​
WebContext.Current.User.Roles
To access the roles the user is assigned to.
​IsInRole​
WebContext.Current.User.IsInRole(string)
To determine whether the authenticated user is a member of a specified role.
The following example shows a domain operation with access restricted to members of a role named Managers.
1
<RequiresRole("Managers")> _
2
Public Function GetCustomers() As IQueryable(Of Customer)
3
Return Me.ObjectContext.Customers
4
End Function
Copied!
1
[RequiresRole("Managers")]
2
public IQueryable<Customer> GetCustomers()
3
{
4
return this.ObjectContext.Customers;
5
}
Copied!
If you call a domain operation when the user does not have the required credentials, the domain operation returns an exception. You can avoid this situation by checking the credentials before calling the domain operation. The following example shows how to check if the user is a member of the required role before loading the data.
1
Private Sub LoadRestrictedReports()
2
Dim loadSales = context.Load(context.GetSalesOrderHeadersQuery().Take(numberOfRows))
3
SalesOrdersGrid.ItemsSource = loadSales.Entities
4
SalesOrdersGrid.Visibility = System.Windows.Visibility.Visible
5
​
6
If (WebContext.Current.User.IsInRole("Managers")) Then
7
Dim loadCustomers = context.Load(context.GetCustomersQuery().Take(numberOfRows))
8
CustomersGrid.ItemsSource = loadCustomers.Entities
9
CustomersGrid.Visibility = System.Windows.Visibility.Visible
10
Else
11
CustomersGrid.Visibility = System.Windows.Visibility.Collapsed
12
End If
13
End Sub
Copied!
1
private void LoadRestrictedReports()
2
{
3
LoadOperation<SalesOrderHeader> loadSales = context.Load(context.GetSalesOrderHeadersQuery().Take(numberOfRows));
4
SalesOrdersGrid.ItemsSource = loadSales.Entities;
5
SalesOrdersGrid.Visibility = System.Windows.Visibility.Visible;
6
​
7
if (WebContext.Current.User.IsInRole("Managers"))
8
{
9
LoadOperation<Customer> loadCustomers = context.Load(context.GetCustomersQuery().Take(numberOfRows));
10
CustomersGrid.ItemsSource = loadCustomers.Entities;
11
CustomersGrid.Visibility = System.Windows.Visibility.Visible;
12
}
13
else
14
{
15
CustomersGrid.Visibility = System.Windows.Visibility.Collapsed;
16
}
17
}
Copied!

Profiles

Profile properties enable you to save information about the user. You can use these properties to customize your application for each user. To use profiles in your solution, you must configure your solution for profiles. For more information, see How to: Enable Profiles in Open Ria Services.
The following methods and properties are typically used when you have implemented profiles.
Member
Code to use
Task
​User​
WebContext.Current.User
To access the object that contains all of the properties that you have added to the User class; for example, User.PhoneNumber.
WebContext.Current.Authentication.LoadUser()
-or-
WebContext.Current.Authentication.LoadUser(LoadUserOperation, object)
To refresh the state of the user.
WebContext.Current.Authentication.SaveUser(boolean)
-or-
WebContext.Current.Authentication.SaveUser(SaveUserOperation, object)
To save any changes in the user state; such as, after setting a profile property value.
The following example shows how to set a user property based on the value selected by the user.
1
Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles OKButton.Click
2
Dim newSelection = Integer.Parse(defaultRows.SelectionBoxItem.ToString())
3
If (newSelection <> WebContext.Current.User.DefaultRows) Then
4
WebContext.Current.User.DefaultRows = newSelection
5
WebContext.Current.Authentication.SaveUser(True)
6
End If
7
Me.DialogResult = True
8
End Sub
Copied!
1
private void OKButton_Click(object sender, RoutedEventArgs e)
2
{
3
int newSelection = int.Parse(defaultRows.SelectionBoxItem.ToString());
4
if (newSelection != WebContext.Current.User.DefaultRows)
5
{
6
WebContext.Current.User.DefaultRows = newSelection;
7
WebContext.Current.Authentication.SaveUser(true);
8
}
9
this.DialogResult = true;
10
}
Copied!

Handling Authentication Errors on the Client

You can handle errors that arise when logging in, logging out, loading, or saving users by providing a callback method as a parameter when you call those methods. In the callback method, you add code to handle the error and call the MarkErrorAsHandled method to specify that the framework will not throw an exception. The AuthenticationService class enables you to provide a callback method when you call the following methods:
The example in the previous "Authentication" section shows a callback method for the Login operation that handles errors.
For more information, see Error Handling on the Client.

Restricting Access to a Domain Service

After you have implemented authentication and roles, you can restrict access on a domain service to only particular users. You apply the following attributes to either the entire domain service or to individual operations on the service. When you apply an attribute to the entire service, it applies to all of the operations.
You can also create your own customized authorization attribute. For more information, see How to: Create a Custom Authorization Attribute.
The following example shows a domain service with three domain operations. The RequiresAuthenticationAttribute and RequiresRoleAttribute attributes are used to restrict access. 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.
1
<EnableClientAccess()> _
2
Public Class AdventureWorksDomainService
3
Inherits LinqToEntitiesDomainService(Of AdventureWorksLT_DataEntities)
4
​
5
<RequiresRole("Managers")> _
6
Public Function GetCustomers() As IQueryable(Of Customer)
7
Return Me.ObjectContext.Customers
8
End Function
9
​
10
Public Function GetProducts() As IQueryable(Of Product)
11
Return Me.ObjectContext.Products
12
End Function
13
​
14
<RequiresAuthentication()> _
15
Public Function GetSalesOrderHeaders() As IQueryable(Of SalesOrderHeader)
16
Return Me.ObjectContext.SalesOrderHeaders
17
End Function
18
End Class
Copied!
1
[EnableClientAccess()]
2
public class AdventureWorksDomainService : LinqToEntitiesDomainService<AdventureWorksLT_DataEntities>
3
{
4
[RequiresRole("Managers")]
5
public IQueryable<Customer> GetCustomers()
6
{
7
return this.ObjectContext.Customers;
8
}
9
​
10
public IQueryable<Product> GetProducts()
11
{
12
return this.ObjectContext.Products;
13
}
14
​
15
[RequiresAuthentication()]
16
public IQueryable<SalesOrderHeader> GetSalesOrderHeaders()
17
{
18
return this.ObjectContext.SalesOrderHeaders;
19
}
20
}
Copied!

See Also

Tasks

Concepts

Last modified 6mo ago