Пример #1
0
        protected override void Seed(NdDbContext context)
        {
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new NdDbContext()));

            foreach (var role in Enum.GetNames(typeof(Role)))
            {
                if (!roleManager.Roles.Any(x => x.Name == role))
                {
                    roleManager.Create(new IdentityRole {
                        Name = role
                    });
                }
            }

            var userManager = new UserManager <NdUser>(new UserStore <NdUser>(new NdDbContext()));

            var devAdmin = new NdUser()
            {
                Email              = "*****@*****.**",
                EmailConfirmed     = true,
                FirstName          = "Tamás",
                Gender             = Gender.Male,
                LastName           = "Tüzes-Kátai",
                MustChangePassword = false,
                Title              = Title.Mr,
                UserName           = "******"
            };

            userManager.Create(devAdmin, "j9up1uuU!");
            userManager.AddToRoles(devAdmin.Id, Role.DevAdmin.ToString());
        }
Пример #2
0
        public async Task <IHttpActionResult> CreateAdmin(CreateAdminDto createAdminDto)
        {
            _logger.Debug(string.Format("Begin. Email: [{0}], FirstName: [{1}], Gender: [{2}], LastName: [{3}], PhoneNumber: [{4}], Title: [{5}], UserName: [{6}]",
                                        createAdminDto.Email,
                                        createAdminDto.FirstName,
                                        createAdminDto.Gender.ToString(),
                                        createAdminDto.LastName,
                                        createAdminDto.PhoneNumber,
                                        createAdminDto.Title.ToString(),
                                        createAdminDto.UserName));
            if (!ModelState.IsValid)
            {
                _logger.Error(string.Format(
                                  "Model state is not valid. ModelState: [{0}]",
                                  string.Join(Environment.NewLine, ModelState.Select(x => string.Format("{0}: {1}", x.Key, x.Value)))));
                return(BadRequest(ModelState));
            }

            var user = new NdUser()
            {
                Email                = createAdminDto.Email,
                EmailConfirmed       = true,
                FirstName            = createAdminDto.FirstName,
                Gender               = createAdminDto.Gender.Value,
                LastName             = createAdminDto.LastName,
                PhoneNumber          = createAdminDto.PhoneNumber,
                PhoneNumberConfirmed = true,
                Title                = createAdminDto.Title.Value,
                UserName             = createAdminDto.UserName
            };

            IdentityResult addUserResult = await NdUserManager.CreateAsync(user, createAdminDto.Password);

            if (!addUserResult.Succeeded)
            {
                _logger.Error(string.Format(
                                  "Create admin failed. Email: [{0}], Reason: [{1}]",
                                  createAdminDto.Email,
                                  string.Join(Environment.NewLine, addUserResult.Errors)));
                return(GetErrorResult(addUserResult));
            }

            IdentityResult addUserToRoleResult = await NdUserManager.AddToRoleAsync(user.Id, "Admin");

            if (!addUserToRoleResult.Succeeded)
            {
                _logger.Error(string.Format(
                                  "Add admin to roles failed. Email: [{0}], Reason: [{1}]",
                                  createAdminDto.Email,
                                  string.Join(Environment.NewLine, addUserResult.Errors)));
                return(GetErrorResult(addUserResult));
            }

            Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));

            _logger.Debug(string.Format("Admin created successfully. Email: [{0}]", createAdminDto.Email));
            return(Created(locationHeader, _factory.Create(user)));
        }
Пример #3
0
        public static CreateUserReturnDto CreateCreateUserReturnDto(this NdUser user, UserManager <NdUser> userManager, HttpRequestMessage request)
        {
            CreateUserReturnDto createUserReturnDto = (CreateUserReturnDto)user.CreateUserInfoDto();

            createUserReturnDto.Roles = userManager.GetRolesAsync(user.Id).Result;
            createUserReturnDto.Url   = (new UrlHelper(request)).Link("GetUserById", new { id = user.Id });

            return(createUserReturnDto);
        }
Пример #4
0
        public static AuthenticationProperties CreateProperties(NdUser user)
        {
            IDictionary <string, string> data = new Dictionary <string, string>
            {
                { "email", user.Email }
            };

            return(new AuthenticationProperties(data));
        }
Пример #5
0
 public TherapistDto CreateTherapist(NdUser ndUser)
 {
     return(new TherapistDto
     {
         Id = ndUser.Id,
         Clinic = ndUser.Clinic,
         FirstName = ndUser.FirstName,
         Gender = ndUser.Gender,
         LastName = ndUser.LastName,
         Title = ndUser.Title
     });
 }
Пример #6
0
 public UserInfoDto CreateUserInfo(NdUser ndUser)
 {
     return(new UserInfoDto
     {
         Id = ndUser.Id,
         Clinic = ndUser.Clinic,
         Email = ndUser.Email,
         FirstName = ndUser.FirstName,
         Gender = ndUser.Gender,
         LastName = ndUser.LastName,
         PhoneNumber = ndUser.PhoneNumber,
         Title = ndUser.Title
     });
 }
Пример #7
0
 public static UserInfoDto CreateUserInfoDto(this NdUser user)
 {
     return(new UserInfoDto()
     {
         Email = user.Email,
         FirstName = user.FirstName,
         Gender = user.Gender,
         Id = user.Id,
         Institute = user.Institute,
         LastName = user.LastName,
         PhoneNumber = user.PhoneNumber,
         Title = user.Title,
         WebPage = user.WebPage
     });
 }
Пример #8
0
 public UserReturnDto Create(NdUser ndUser)
 {
     return(new UserReturnDto
     {
         Url = _UrlHelper.Link("GetUserById", new { id = ndUser.Id }),
         Id = ndUser.Id,
         Email = ndUser.Email,
         Roles = _NdUserManager.GetRolesAsync(ndUser.Id).Result,
         Clinic = ndUser.Clinic,
         FirstName = ndUser.FirstName,
         Gender = ndUser.Gender,
         LastName = ndUser.LastName,
         PhoneNumber = ndUser.PhoneNumber,
         Title = ndUser.Title
     });
 }
Пример #9
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var allowedOrigin = "*";

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });

            var    userManager = context.OwinContext.GetUserManager <NdUserManager>();
            NdUser user        = await userManager.FindByEmailAsync(context.UserName);

            if (user != null)
            {
                user = await userManager.FindAsync(user.UserName, context.Password);
            }
            else
            {
                user = await userManager.FindAsync(context.UserName, context.Password);
            }


            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                _logger.Debug(string.Format("The user name or password is incorrect [username: {0}, password: {1}]", context.UserName, context.Password));
                return;
            }

            if (!user.EmailConfirmed)
            {
                context.SetError("invalid_grant", "User did not confirm email.");
                _logger.Debug(string.Format("User did not confirm email [username: {0}]", context.UserName));
                return;
            }

            ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");

            AuthenticationProperties properties = CreateProperties(user);
            var ticket = new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);

            _logger.Debug(string.Format("User logged in [username: {0}, password: {1}]", context.UserName, context.Password));
        }
        protected override void Seed(NdDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            var userManager = new UserManager <NdUser>(new UserStore <NdUser>(new NdDbContext()));
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new NdDbContext()));

            var user = new NdUser()
            {
                UserName             = "******",
                Email                = "*****@*****.**",
                EmailConfirmed       = true,
                PhoneNumber          = "+40745024467",
                PhoneNumberConfirmed = true,
                FirstName            = "Developer",
                LastName             = "Admin",
                Gender               = Gender.Male,
                Title                = Title.Mr
            };

            userManager.Create(user, "j9up1uuU!");

            foreach (var role in Enum.GetNames(typeof(Role)))
            {
                if (!roleManager.Roles.Any(x => x.Name == role))
                {
                    roleManager.Create(new IdentityRole {
                        Name = role
                    });
                }
            }

            var devAdminUser = userManager.FindByName("devadmin");

            userManager.AddToRoles(devAdminUser.Id, Role.DevAdmin.ToString());
        }
Пример #11
0
        public async Task <IHttpActionResult> CreateTherapist(CreateTherapistDto createTherapistDto)
        {
            _logger.Debug(string.Format("Begin. Clinic: [{0}], Email: [{1}], FirstName: [{2}], Gender: [{3}], LastName: [{4}], PhoneNumber: [{5}], Title: [{6}]",
                                        createTherapistDto.Clinic,
                                        createTherapistDto.Email,
                                        createTherapistDto.FirstName,
                                        createTherapistDto.Gender.ToString(),
                                        createTherapistDto.LastName,
                                        createTherapistDto.PhoneNumber,
                                        createTherapistDto.Title.ToString()));
            if (!ModelState.IsValid)
            {
                _logger.Error(string.Format(
                                  "Model state is not valid. ModelState: [{0}]",
                                  string.Join(Environment.NewLine, ModelState.Select(x => string.Format("{0}: {1}", x.Key, x.Value)))));
                return(BadRequest(ModelState));
            }

            var user = new NdUser()
            {
                Clinic      = createTherapistDto.Clinic,
                Email       = createTherapistDto.Email,
                FirstName   = createTherapistDto.FirstName,
                Gender      = createTherapistDto.Gender,
                LastName    = createTherapistDto.LastName,
                PhoneNumber = createTherapistDto.PhoneNumber,
                Title       = createTherapistDto.Title,
                UserName    = createTherapistDto.Email
            };

            var            password      = PasswordGenerator.Generate();
            IdentityResult addUserResult = await NdUserManager.CreateAsync(user, password);

            if (!addUserResult.Succeeded)
            {
                _logger.Error(string.Format(
                                  "Create user failed. Email: [{0}], Reason: [{1}]",
                                  createTherapistDto.Email,
                                  string.Join(Environment.NewLine, addUserResult.Errors)));
                return(GetErrorResult(addUserResult));
            }

            IdentityResult addUserToRoleResult = await NdUserManager.AddToRoleAsync(user.Id, "Therapist");

            if (!addUserToRoleResult.Succeeded)
            {
                _logger.Error(string.Format(
                                  "Add user to roles failed. Email: [{0}], Reason: [{1}]",
                                  createTherapistDto.Email,
                                  string.Join(Environment.NewLine, addUserResult.Errors)));
                return(GetErrorResult(addUserResult));
            }

            try
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Path.Combine("~/Results", user.Id)));
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Error creating folder for therapist. Email: [{0}]", createTherapistDto.Email), ex);
                return(InternalServerError(ex));
            }

            try
            {
                string code = await NdUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));
                await NdUserManager.SendEmailAsync(user.Id, "Confirm your account", NdEmailService.CreateConfirmEmailWithPasswordBody(callbackUrl.ToString(), password));
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Error sending ConfirmEmail email for therapist. Email: [{0}]", createTherapistDto.Email), ex);
                return(InternalServerError(ex));
            }

            Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));

            return(Created(locationHeader, _factory.Create(user)));
        }