2011-01-21 58 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。这将从服务器加载经过身份验证的用户。

我想我会发布这个,如果任何人偶然遇到同样的问题。