WCF RIA Service自定义登录验证
2010-07-28 14:35 Creative dream 阅读(2165) 评论(6) 收藏 举报本文更改WCF RIA Service登录验证,以实现以数据库中用户为验证依据,而非VS中的ASP.net Configration,下图中所示

- 开发工具:VS 2010 EN
- 开发语言:Visual C#
- WCF RIA Service 1.0
- Silverlight 4.0
在Silverlight中用户验证部分通过Authentication Domain Service实现,如下图

具体添加方法不做介绍,添加完成后会生成两个类:RIAAuthenticationService(名称根据实际而定),继承自 AuthenticationBase<User>,
和User,继承自UserBase,具体代码如下:
[EnableClientAccess]
public class RIAAuthenticationService : 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; }
}
在默认情况下WebContext.Current.Authentication.Login方法指向的是ASP.net Configration中定义的用户验证,如果我们要将Login方法指向我们的数据用户表,经过测试,在服务器端RIAAuthenticationService类中重载ValidateUser(string userName,string password)方法,将其中逻辑判断数据库中的用户有效性,返回bool值就可以了。具体代码见下:
[EnableClientAccess]
public class RIAAuthenticationService : AuthenticationBase<User>
{
// To enable Forms/Windows Authentication for the Web Application,
// edit the appropriate section of web.config file.
protected override bool ValidateUser(string userName, string password)
{
RIAService riaSvr = new RIAService();
if (riaSvr.GetSysUserInfo(userName, password) != null)
return true;
else
return false;
}
}
以上只是测试用户登录,具体角色(Roles)验证部分没有涉及。
浙公网安备 33010602011771号