views:

55

answers:

1

My SL app use windowes authentication.

When app started, Following code add user info to colloection:

 public App()
    {
        InitializeComponent();

        // Create a WebContext and add it to the ApplicationLifetimeObjects
        // collection.  This will then be available as WebContext.Current.

        WebContext webContext = new WebContext();
        webContext.Authentication = new WindowsAuthentication() { DomainContext = new AMSRIAServices.Web.AuthenticationContext() };
        this.ApplicationLifetimeObjects.Add(webContext);
    }

Then in any code-behind of user control, I want to get back the user info. I tried to use WebContext.Current.User, but I get nothing. How to get user info from Application.Current.ApplicationLifetimeObjects in code?

A: 

The typical pattern would be to include a static property of called Current on your context type then assign that during the StartService method.

public class MyWebContext : IAppicationService
{

   public MyWebContext Current {get; private set}


   public void StartService(ApplicationServiceContext context)
   {
      Current = this;
      // Other initialisation code
   }
   // Rest of implementation

}

Hence accessing your webContext object is simply:-

MyWebContext.Current.DoStuff();
AnthonyWJones
Thanks. I already have WebContext and set it in app starting point(see post, I add the app()). It is available for SL app. I want to know how to get it.
KentZhou
@KentZhou: in that case it will already have derived from WebContextBase and have a `Current` property. I don't really know enough about WCF RIA Services to know why the User isn't configured yet, it could be an asynchronous thing or that Login needs to happen first.
AnthonyWJones