如何从WebContext获取用户信息?

如何从WebContext获取用户信息?

问题描述:

我的SL应用程序使用窗口身份验证。如何从WebContext获取用户信息?

当应用程序启动时,下面的代码添加用户信息到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); 
    } 

然后在用户控件的任何代码隐藏,我想找回用户信息。我试图使用WebContext.Current.User,但我什么都没有。如何从代码中的Application.Current.ApplicationLifetimeObjects获取用户信息?

典型模式是在您的上下文类型中包含一个名为Current的静态属性,然后在StartService方法中分配该属性。

public class MyWebContext : IAppicationService 
{ 

    public MyWebContext Current {get; private set} 


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

} 

因此访问您webContext目标很简单: -

MyWebContext.Current.DoStuff(); 
+0

谢谢。我已经有WebContext并将其设置在应用程序的起点(请参阅后,我添加应用程序())。它可用于SL应用程序。我想知道如何得到它。 – KentZhou 2010-07-29 17:38:50

+0

@KentZhou:在这种情况下,它已经从WebContextBase派生并具有'Current'属性。我对WCF RIA Services知之甚少,不知道为什么用户还没有配置好,它可能是一个异步的事情,或者登录需要首先发生。 – AnthonyWJones 2010-07-29 21:22:42

我想在Application_Startup你需要做一个LoadUser:

WebContext.Current.Authentication.LoadUser();

虽然我有类似的问题让用户加载。但我正在使用formsauthentication。如果您创建一个Silverlight商业应用程序并检查app.xaml.cs,您可以看到如何执行此操作的示例。

让我知道它是否适用于Windows身份验证。