如何从WebContext获取用户信息?

时间:2010-07-29 15:59:57

标签: silverlight silverlight-4.0

我的SL应用使用windowes身份验证。

当应用开始时,以下代码将用户信息添加到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获取用户信息?

2 个答案:

答案 0 :(得分:0)

典型的模式是在您的上下文类型中包含一个名为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();

答案 1 :(得分:0)

我认为在Application_Startup中你需要做一个LoadUser:

WebContext.Current.Authentication.LoadUser();

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

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

相关问题