In my silverlight application I'm using wcf ria services to handle my authentication. I wanted to add a custom property and method to my User object and have it be available to my silverlight client.
RiaContext.Current.User.MyNewProperty
RiaContext.Current.User.MyNewMethod.
The default silverlight business application template will create a AuthenticationService.cs file for you.
[EnableClientAccess]
public class AuthenticationService : AuthenticationBase<User>
{
}
public class User : UserBase
{
// NOTE: Profile properties can be added for use in Silverlight application.
// To enable profiles, edit the appropriate section of web.config file.
}
First I added my property to my user class and added an attribute to exclude it from profile usage
public class User : UserBase
{
[ProfileUsage(IsExcluded = true)]
public IEnumerable<string> Permissions { get; private set; }
}
I didn't want this property to be saved as part of the users profile back to a database so you can set this property attribute if you don't want to setup a profile provider in your web.config. Next I tried adding my custom method to my user class but when I went to access the property in my silverlight client, the method was not showing up. Since we are using wcf ria services, if you want methods to show up on the client then you have to explictly say so by creating a *.shared.cs class.
So to get everything working:
First you will want to move your User class out ouf the AuthenticationService.cs file and into its own User.cs file.
Next, delare your User class as a partial class. This is so we can add our custom method via a shared file.
Create another file called User.Shared.cs and declare this as a partial class as well along with your custom method.
Now when everything gets compiled and generated, your custom property and custom method will be available on your silverlight client user object.
Here is what each file should look like.
AuthenticationService.cs
[EnableClientAccess]
public class AuthenticationService : AuthenticationBase<User>
{
}
User.cs
public partial class User : UserBase
{
public User()
{
List<string> permissions = new List<string>();
permissions.Add("test");
permissions.Add("test2");
Permissions = permissions;
}
// NOTE: Profile properties can be added for use in Silverlight application.
// To enable profiles, edit the appropriate section of web.config file.
[ProfileUsage(IsExcluded = true)]
public IEnumerable<string> Permissions { get; private set; }
}
User.Shared.cs
public partial class User
{
public bool HasPermission(string permission)
{
if ((this.Permissions == null))
{
return false;
}
return System.Linq.Enumerable.Contains(Permissions, permission);
}
}
Build your app and in your silverlight client you will have your new property and method
RiaContext.Current.User.Permissions
RiaContext.Current.User.HasPermissions