Вдобавок к тому, что функция членства может изменить глобальные данные, для функции членства возможно изменить явно объявленных изменяемых членов рассматриваемого объекта.
Если вы создадите «бизнес-приложение Silverlight» и увидите, как шаблон реализует аутентификацию. (Или просто перейдите сюда и загрузите образец проекта шаблона .)
Чтобы упростить, вот процесс, который я использовал:
Во-первых, я создаю службу домена (FooService), производную от LinqToEntitiesDomainService, где FooContext это моя модель сущности. В нем я добавляю все операции CRUD для доступа к моей настраиваемой таблице БД и возврата профилей пользователей.
Затем создайте конкретный класс User на стороне сервера, унаследовав его от 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);
}
}