WCF Ria Services - Authentication - Add custom property and method to authenticated user

February 25, 2010 at 11:24 PMdsoltesz

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

Posted in: .NET | c# | silverlight | wcf ria services

Tags: , , ,

Comments (3) -

nikitakova
Vietnam nikitakova says:

Hi Dan,

I'm using WCF RIA Service .NET 3.5 for building an silverlight application. Now I'm facing a problems in remembering parameter form values.

I've used user profile to remember theme but I realize that has many limits.

At first, it doesn't have profile group like in ASP.NET for grouping each report parameter form values.

Second, it doesn't support custom property.

So when I've multiple parameter forms, i must add many properties (same same) to profile and user class.

I don't know anyone here have better solution for this?

If you have please show me.I need a architecture for remembering many parameter form values or values in system option.

Beside I have an other solution is using database to store params like (user ID, form ID, value ID, value) but I don't know which better.

Thanks you very much.

Reply

If you need to store use specific information about one of your silverlight pages/forms then you could look into using Isolated Storage.

Reply

Nice to be stumbling up to your site again, it has been months for me. Anyway, this is the article that i've been waiting for so long. I need this article to end my assignment in the school, and it has same topic with your post. Thank you, excellent share.

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading