views:

341

answers:

1

I'm writing a silverlight application that resembles a shopping cart system. This app can only be launched from the asp.net website after the user is logged in to the site.

Upon first load, the app will send a request to the backend through WCF service to retreive all the existing items in the shopping cart.

Therefore the silverlight app needs to know who the current user is, to find which shopping cart to load.

I found there are a couple of ways so far, but not happy with any of them:

  1. using wcf aspnet compat. silverlight can ask who the current user is by asking the wcf service.
  2. pass parameters from the page to xaml by using xaml.InitParameters and pass in the minimum amount of information to identify a user in a serialized format.
  3. pass parameters through query string to xaml (apparently this is also possible)

Can anyone share the best practice to achieve this? Thanks

+2  A: 

We use the first solution in our projects. You haven't to invent any type of serialization format or so in this case. A disadvantage of this approach - extra async logic at startup.

The example of service:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class UserInfoService : IUserInfoService
{
        public UserInfo GetUserInfo()
        {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                        return null;

                var userInfo = new UserInfo
                {
                        Login = HttpContext.Current.User.Identity.Name,
                        Fullname = ...,
                };

                return userInfo;
        }
}

Sending userid via initParams or query string is not good idea I think. Such things should be more hidden.

The real important thing is to verify user on server on each service call because anyone can call your services in similar way as your app.

HTH

Alexander K.