2011-01-21 55 views
1

我目前正在試用Silverlight和RIA服務。我正在實施一個簡單的登錄表單。我也使用提供的身份驗證域服務模板生成以下文件:Silverlight和RIA服務:在會話中持續登錄

[EnableClientAccess] 
public class AuthenticationDomainService : AuthenticationBase<User> 
{ 
    // To enable Forms/Windows Authentication for the Web Application, 
    // edit the appropriate section of web.config file. 
} 

public class User : UserBase 
{ 
    // NOTE: Profile properties can be added here 
    // To enable profiles, edit the appropriate section of web.config file. 

    // public string MyProfileProperty { get; set; } 
    public int DefaultRows { get; set; } 
} 

現在我可以在我的應用程序中沒有問題地登錄/註銷。在Silverlight應用程序中,登錄後,行:

WebContext.Current.User.IsAuthenticated; 

返回true。

但是,我需要堅持跨會話(即當我使用F5重新加載頁面)。

當前,當頁面重新加載時,我必須重新登錄。

這裏是我的登錄代碼:

WebContext.Current.Authentication.Login(new LoginParameters(this.UserName, this.Password, true, string.Empty), 
        (LoginOperation loginOperation) => 
        { 
         if (loginOperation.LoginSuccess) 
         { 
          NotificationMessage Message = new NotificationMessage(this, null, "CLOSE"); 
          Messenger.Default.Send<NotificationMessage>(Message); 
         } 
        }, null); 

登錄方法的第三個參數是IsPersistent參數。從MSDN Docs中,我認爲將其設置爲true時,下次加載頁面時,用戶仍將登錄。但是,情況並非如此。

我是否需要閱讀已在內部設置的cookie,然後使用該cookie提供的用戶名/密碼在後臺登錄?或者在這裏工作還有其他魔法嗎?

我希望以某種方式已經做到了這一點。

在此先感謝

回答

4

通過Silverlight業務應用程序模板去後,我發現這行代碼:

WebContext.Current.Authentication.LoadUser(this.Application_UserLoaded, null); 

一個好地方,把它在裏面App.xaml中應用程序的啓動事件的.cs。這將從服務器加載經過身份驗證的用戶。

我想我會發布這個,如果任何人偶然遇到同樣的問題。