如果创建“Silverlight业务应用程序”,您将看到模板如何实现身份验证。(或者仅仅去
here and download the template sample project
)
为了简化,我使用了以下过程:
首先,我创建了一个从linqtoentiesdomainservice派生的域服务(fooservice),其中fooContext是我的实体模型。在其中,我添加了所有CRUD操作来访问我的自定义DB表并返回用户配置文件。
接下来,通过从userbase派生,在服务器端创建一个具体的用户类:
using System.Web.Ria;
using System.Web.Ria.ApplicationServices;
public class User : UserBase
{}
最后,从authenticationBase派生一个类并实现以下四个方法:
[EnableClientAccess]
public class AuthenticationService : AuthenticationBase<User>
{
private FooService _service = new FooService();
protected override bool ValidateUser(string username, string password)
{
// Code here that tests only if the password is valid for the given
// username using your custom DB calls via the domain service you
// implemented above
}
protected override User GetAuthenticatedUser(IPrincipal pricipal)
{
// principal.Identity.Name will be the username for the user
// you're trying to authenticate. Here's one way to implement
// this:
User user = null;
if (this._service.DoesUserExist(principal.Identity.Name)) // DoesUserExist() is a call
// added in my domain service
{
// UserProfile is an entity in my DB
UserProfile profile = this._service.GetUserProfile(principal.Identity.Name);
user.Name = profile.UserName;
user.AuthenticationType = principal.Identity.AuthenticationType;
}
return user;
}
public override void Initialize(DomainServiceContext context)
{
this._service.Initialize(context);
base.Initialize(context);
}
protected override void Dispose(bool disposing)
{
if (disposing)
this._service.Dispose();
base.Dispose(disposing);
}
}