public void Database_Should_Always_Be_Created()
        {
            Database.SetInitializer( new DropCreateDatabaseAlways<NwdBackOfficeContext>() );
            Database.SetInitializer( new DropCreateDatabaseAlways<NwdFrontOfficeContext>() );
            Database.SetInitializer( new DropCreateDatabaseAlways<NwdAuthContext>() );

            using( var b = new NwdBackOfficeContext() )
            {
                b.Database.Initialize( true );
                Debug.Assert( b.Database.Exists() );
                Console.WriteLine( b.Database.Connection.ConnectionString );
            }
            using( var f = new NwdFrontOfficeContext() )
            {
                f.Database.Initialize( true );
                Debug.Assert( f.Database.Exists() );
                Console.WriteLine( f.Database.Connection.ConnectionString );
            }
            using( var a = new NwdAuthContext() )
            {
                a.Database.Initialize( true );
                Debug.Assert( a.Database.Exists() );
                Console.WriteLine( a.Database.Connection.ConnectionString );
                a.Roles.Add( new Role { RoleName = "User" } );
                Role r = a.Roles.Add( new Role { RoleName = "Administrator" } );
                Nwd.Authentication.Model.User u = a.Users.Add( new Nwd.Authentication.Model.User { Username = "******", Name = "NwdProvider", Comment = "user admin", IsApproved = true, IsLockedOut = false, Password = AuthenticationUtils.HashPassword( "test" ), CreationDate = DateTime.UtcNow } );
                u.Roles.Add( r );

                a.SaveChanges();
            }
        }
Exemplo n.º 2
0
        public override bool DeleteRole( string roleName, bool throwOnPopulatedRole )
        {
            using( var c = new NwdAuthContext() )
            {
                Role r = c.Roles.Where( m => m.RoleName == roleName ).FirstOrDefault();
                if( r == null ) return false;

                c.Roles.Remove( r );
                c.SaveChanges();
                return true;
            }
        }
Exemplo n.º 3
0
 public ActionResult Register( RegisterViewModel model )
 {
     if( ModelState.IsValid )
     {
         using( var ctx = new NwdAuthContext() )
         {
             if( ctx.Users.Any( m => m.Username == model.Username ) )
                 ModelState.AddModelError( "Username", "Ce nom d'utilisateur existe déjà !" );
             else
             {
                 try
                 {
                     User u = ctx.Users.Add( new User { Username = model.Username, Name = "NwdProvider", Password = AuthenticationUtils.HashPassword( model.Password ), Email = model.Email, CreationDate = DateTime.UtcNow } );
                     u.Roles.Add( ctx.Roles.Where( r => r.RoleName == "User" ).FirstOrDefault() );
                     ctx.SaveChanges();
                     return View( "RegisterSuccess" );
                 }
                 catch( MembershipCreateUserException ex )
                 {
                     ModelState.AddModelError( "", ex.Message );
                 }
             }
         }
     }
     return View( model );
 }
Exemplo n.º 4
0
        public override bool ChangePassword( string username, string oldPassword, string newPassword )
        {
            using( var c = new NwdAuthContext() )
            {
                User u = GetUser( username, c );

                if( u == null ) return false;
                if( u.Password != EncodePassword( oldPassword ) ) return false;

                u.Password = EncodePassword( newPassword );
                c.Users.Attach( u );
                c.Entry( u ).Property( m => m.Password ).IsModified = true;
                c.SaveChanges();

                return true;
            }
        }
Exemplo n.º 5
0
        public override void UpdateUser( MembershipUser user )
        {
            using( var c = new NwdAuthContext() )
            {
                User u = c.Users.Where( m => m.Username == user.UserName ).FirstOrDefault();
                if( u == null ) return;

                c.Users.Attach( u );

                if( user.Email != u.Email )
                {
                    u.Email = user.Email;
                    c.Entry( u ).Property( m => m.Email ).IsModified = true;
                }

                if( user.ProviderName != u.Name )
                {
                    u.Name = user.ProviderName;
                    c.Entry( u ).Property( m => m.Email ).IsModified = true;
                }

                c.SaveChanges();
            }
        }
Exemplo n.º 6
0
        public override MembershipUser CreateUser( string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status )
        {
            if( username == null || username.Length > 0 ) { status = MembershipCreateStatus.InvalidEmail; return null; }
            if( password == null || password.Length > 0 ) { status = MembershipCreateStatus.InvalidPassword; return null; }

            using( var c = new NwdAuthContext() )
            {
                if( GetUser( username, c ) != null ) { status = MembershipCreateStatus.DuplicateUserName; return null; }
                User u = new User { Username = username, Password = password, Email = email };
                c.Users.Add( u );
                c.SaveChanges();
                status = MembershipCreateStatus.Success;
                return GetUser( username, false );
            }
        }