private static void ResetAll(Container container, OrmLiteAuthRepository authRepo) { authRepo.DropAndReCreateTables(); container.Resolve<IDbConnectionFactory>().Run(db => { db.DropAndCreateTable<EmailRegistration>(); db.DropAndCreateTable<SMessageReceipt>(); }); container.Resolve<IRedisClientsManager>().Exec(r => r.FlushAll()); }
public void SetUp() { try { tests = new OAuthUserSessionTests(); var inMemoryRepo = new InMemoryAuthRepository(); inMemoryRepo.Clear(); userAuthRepositorys.Add(inMemoryRepo); var appSettings = new AppSettings(); var redisRepo = new RedisAuthRepository(new BasicRedisClientManager(new string[] { appSettings.GetString("Redis.Host") ?? "localhost" })); redisRepo.Clear(); userAuthRepositorys.Add(redisRepo); if (OAuthUserSessionTestsBase.UseSqlServer) { var connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\auth.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; var sqlServerFactory = new OrmLiteConnectionFactory(connStr, SqlServerOrmLiteDialectProvider.Instance); var sqlServerRepo = new OrmLiteAuthRepository(sqlServerFactory); sqlServerRepo.DropAndReCreateTables(); } else { var sqliteInMemoryRepo = new OrmLiteAuthRepository(dbFactory); dbFactory.Run(db => { db.CreateTable<UserAuth>(true); db.CreateTable<UserOAuthProvider>(true); }); sqliteInMemoryRepo.Clear(); userAuthRepositorys.Add(sqliteInMemoryRepo); var sqliteDbFactory = new OrmLiteConnectionFactory( "~/App_Data/auth.sqlite".MapProjectPath()); var sqliteDbRepo = new OrmLiteAuthRepository(sqliteDbFactory); sqliteDbRepo.CreateMissingTables(); userAuthRepositorys.Add(sqliteDbRepo); } } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }
private void ConfigureAuth(Container container){ container.Register<ICacheClient>(new MemoryCacheClient()); Plugins.Add(new AuthFeature( () => new AuthUserSession(), // or Use your own typed Custom AuthUserSession type new IAuthProvider[] { new CredentialsAuthProvider() })); var appSettings = new ConfigurationResourceManager(); var dbFactory = new OrmLiteConnectionFactory(ConfigUtils.GetConnectionString("UserAuth")) ; OrmLiteAuthRepository authRepo = new OrmLiteAuthRepository( dbFactory ); container.Register<IUserAuthRepository>( c => authRepo ); //Use OrmLite DB Connection to persist the UserAuth and AuthProvider info if (appSettings.Get("RecreateAuthTables", false)) authRepo.DropAndReCreateTables(); //Drop and re-create all Auth and registration tables else{ authRepo.CreateMissingTables(); //Create only the missing tables } Plugins.Add( new RegistrationFeature()); //Add admin user string userName = "******"; string password = "******"; List<string> userPermissions= new List<string>( new string[]{ "Customer.create", "Company.create", "Country.create", "City.create", "Author.create", "Person.create", "Customer.read", "Company.read", "Country.read", "City.read", "Author.read", "Person.read", "Customer.update", "Company.update", "Country.update", "City.update", "Author.update", "Person.update" }); List<string> adminPermissions= new List<string>(userPermissions); adminPermissions.AddRange(new string[]{ "Customer.destroy","Company.destroy","Country.destroy","City.destroy","Author.destroy","Person.destroy" }); if ( authRepo.GetUserAuthByUserName(userName)== default(UserAuth) ){ List<string> roles= new List<string>(); roles.Add(RoleNames.Admin); string hash; string salt; new SaltedHash().GetHashAndSaltString(password, out hash, out salt); authRepo.CreateUserAuth(new UserAuth { DisplayName = userName, Email = userName+"@mail.com", UserName = userName, FirstName = "", LastName = "", PasswordHash = hash, Salt = salt, Roles =roles, Permissions=adminPermissions, }, password); } // user userName="******"; password="******"; var meta= new Dictionary<string,string>(); meta.Add("ExpiresAt", DateTime.UtcNow.SerializeToString()); if ( authRepo.GetUserAuthByUserName(userName)== default(UserAuth) ){ string hash; string salt; new SaltedHash().GetHashAndSaltString(password, out hash, out salt); authRepo.CreateUserAuth(new UserAuth { DisplayName = userName, Email = userName+"@mail.com", UserName = userName, FirstName = "", LastName = "", PasswordHash = hash, Salt = salt, Permissions=userPermissions, Meta= meta }, password); } }
private void ConfigureAuth(Container container){ var appSettings = new ConfigurationResourceManager(); double se= appSettings.Get("DefaultSessionExpiry", 480); AuthProvider.DefaultSessionExpiry=TimeSpan.FromMinutes(se); if (appSettings.Get("EnableRedisForAuthCache", false)){ string cacheHost= appSettings.Get("AuthCacheHost", "localhost:6379"); int cacheDb= appSettings.Get("AuthCacheDb",8); string cachePassword= appSettings.Get("AuthCachePassword",string.Empty); var p = new PooledRedisClientManager(new string[]{cacheHost}, new string[]{cacheHost}, cacheDb); if(! string.IsNullOrEmpty(cachePassword)) p.GetClient().Password= cachePassword; container.Register<ICacheClient>(p); } else { container.Register<ICacheClient>(new MemoryCacheClient()); } Plugins.Add(new AuthFeature( () => new AuthUserSession(), // or Use your own typed Custom AuthUserSession type new IAuthProvider[] { new AuthenticationProvider(){SessionExpiry=TimeSpan.FromMinutes(se)} }) { IncludeAssignRoleServices=false, }); var dbFactory = new OrmLiteConnectionFactory(ConfigUtils.GetConnectionString("UserAuth")) ; OrmLiteAuthRepository authRepo = new OrmLiteAuthRepository( dbFactory ); container.Register<IUserAuthRepository>( c => authRepo ); //Use OrmLite DB Connection to persist the UserAuth and AuthProvider info if (appSettings.Get("EnableRegistrationFeature", false)) Plugins.Add( new RegistrationFeature()); if (!appSettings.Get("AddUsers", false)) return; // addusers var oldL =FirebirdOrmLiteDialectProvider.Instance.DefaultStringLength; FirebirdOrmLiteDialectProvider.Instance.DefaultStringLength=1024; if (appSettings.Get("RecreateAuthTables", false)) authRepo.DropAndReCreateTables(); //Drop and re-create all Auth and registration tables else{ authRepo.CreateMissingTables(); //Create only the missing tables } FirebirdOrmLiteDialectProvider.Instance.DefaultStringLength=oldL; //Add admin user string userName = "******"; string password = "******"; List<string> permissions= new List<string>( new string[]{ }); if ( authRepo.GetUserAuthByUserName(userName)== default(UserAuth) ){ List<string> roles= new List<string>(); roles.Add(RoleNames.Admin); string hash; string salt; new SaltedHash().GetHashAndSaltString(password, out hash, out salt); authRepo.CreateUserAuth(new UserAuth { DisplayName = userName, Email = userName+"@mail.com", UserName = userName, FirstName = "", LastName = "", PasswordHash = hash, Salt = salt, Roles =roles, Permissions=permissions }, password); } userName = "******"; password = "******"; permissions= new List<string>( new string[]{ }); if ( authRepo.GetUserAuthByUserName(userName)== default(UserAuth) ){ List<string> roles= new List<string>(); roles.Add("Test"); string hash; string salt; new SaltedHash().GetHashAndSaltString(password, out hash, out salt); authRepo.CreateUserAuth(new UserAuth { DisplayName = userName, Email = userName+"@mail.com", UserName = userName, FirstName = "", LastName = "", PasswordHash = hash, Salt = salt, Roles =roles, Permissions=permissions }, password); } }