public override void Configure(Funq.Container container) { Plugins.Add(new AuthFeature( () => new AuthUserSession(), new IAuthProvider[] { new BasicAuthProvider() })); Plugins.Add(new RegistrationFeature()); container.Register<ICacheClient>(new MemoryCacheClient()); var userRepository = new InMemoryAuthRepository(); container.Register<IUserAuthRepository>(userRepository); }
Skonfigurowany został jeden z gotowych providerów, BasicProvider. Programiści mają też możliwość definiowania własnych providerów. RegistrationFeature umożliwi REST-owe definiowanie ról. Repozytorium pacjentów trzymane będzie w pamięci. W tym samym miejscu możemy dodać pierwszego użytkownika z rolą Admin
string hash, salt; new SaltedHash().GetHashAndSaltString("password", out hash, out salt); userRepository.CreateUserAuth(new UserAuth() { Id = 1, FirstName = "John", Email = "jdoe@gmail.com", PasswordHash = hash, UserName = "jdoe", LastName = "Doe", Salt = salt, Roles = new List<string>(){RoleNames.Admin}, Permissions = new List<string>(){"GetEntry"} }, "password");
Wymagane role i uprawnienia a także konieczność autentykacji deklarujemy atrybutami.
[Route("/entry")] [Authenticate] [RequiredRole("User")] public class Entry : IReturn<EntryResponse> { public DateTime Date { get; set; } public int Count { get; set; } }
Logowanie i przydzielanie ról użytkownikom (w szczególnym przypadku samemu sobie) wykonuje się w poniższy sposób
var client = new JsonServiceClient("http://localhost:51270"){UserName = "jdoe", Password = "password"}; client.Send<AssignRolesResponse>(new AssignRoles() { UserName = "jdoe", Roles = new List<string>(){"User"}, Permissions = new List<string>(){"GetEntry"} });
Brak komentarzy:
Prześlij komentarz