public async Task<IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var user = new ApplicationUser()
            {
                UserName = createUserModel.Username,
                Email = createUserModel.Email,
                FirstName = createUserModel.FirstName,
                LastName = createUserModel.LastName,
                Level = 3,
                JoinDate = DateTime.Now.Date,
            };

            IdentityResult addUserResult = await this.AppUserManager.CreateAsync(user, createUserModel.Password);

            if (!addUserResult.Succeeded)
            {
                return GetErrorResult(addUserResult);
            }

            //generate token and send confirmation mail
            string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

            var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));

            await this.AppUserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

            Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));
            
            return Created(locationHeader, TheModelFactory.Create(user));
        }
        public async Task<IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var user = new ApplicationUser()
            {
                UserName = createUserModel.Username,
                Email = createUserModel.Email,
                FirstName = createUserModel.FirstName,
                LastName = createUserModel.LastName,
                Level = 3,
                JoinDate = DateTime.Now.Date,
            };

            IdentityResult addUserResult = await this.AppUserManager.CreateAsync(user, createUserModel.Password);

            if (!addUserResult.Succeeded)
            {
                return GetErrorResult(addUserResult);
            }

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

            return Created(locationHeader, TheModelFactory.Create(user));
        }
Ejemplo n.º 3
0
 private async Task<string> GenerateUserToken(ApplicationUser validatedUser)
 {
     CustomJwtFormat jwt = new CustomJwtFormat("http://jv.com");
     var identity = await validatedUser.GenerateUserIdentityAsync(AppUserManager, "password");
     Microsoft.Owin.Security.AuthenticationProperties properties = new Microsoft.Owin.Security.AuthenticationProperties();
     properties.IssuedUtc = DateTime.Now.ToUniversalTime();
     properties.ExpiresUtc = DateTime.Now.AddMinutes(5).ToUniversalTime();
     return jwt.Protect(new Microsoft.Owin.Security.AuthenticationTicket(identity, properties));
 }
Ejemplo n.º 4
0
 public UserReturnModel Create(ApplicationUser appUser)
 {
     return new UserReturnModel
     {
         Url = _UrlHelper.Link("GetUserById", new { id = appUser.Id }),
         Id = appUser.Id,
         UserName = appUser.UserName,
         FullName = string.Format("{0} {1}", appUser.FirstName, appUser.LastName),
         Email = appUser.Email,
         EmailConfirmed = appUser.EmailConfirmed,
         Level = appUser.Level,
         JoinDate = appUser.JoinDate,
         Roles = _AppUserManager.GetRolesAsync(appUser.Id).Result,
         Claims = _AppUserManager.GetClaimsAsync(appUser.Id).Result
     };
 }
        public static IEnumerable<Claim> GetClaims(ApplicationUser user)
        {
            List<Claim> claims = new List<Claim>();

            var daysInWork = (DateTime.Now.Date - user.JoinDate).TotalDays;

            if(daysInWork > 90)
            {
                claims.Add(CreateClaim("FTE", "1"));
            }
            else
            {
                claims.Add(CreateClaim("FTE", "0"));
            }
            return claims;
        }
Ejemplo n.º 6
0
 public AffiliateReturnModel CreateAffiliate(ApplicationUser appUser, UserExtension extension)
 {
     return new AffiliateReturnModel
     {
         Email = appUser.Email,
         FirstName = appUser.FirstName,
         LastName = appUser.LastName,
         PhoneNumber = appUser.PhoneNumber,
         Username = appUser.UserName,
         IndividualDescription = extension.IndividualDescription,
         SkypeHandle = extension.SkypeHandle
     };
 }
Ejemplo n.º 7
0
 public ExtendedUserReturnModel Create(ApplicationUser appUser, UserExtension extension)
 {
     return new ExtendedUserReturnModel
     {
         Url = _UrlHelper.Link("GetUserById", new { id = appUser.Id }),
         Id = appUser.Id,
         UserName = appUser.UserName,
         FullName = string.Format("{0} {1}", extension.FirstName, extension.LastName),
         FirstName = extension.FirstName,
         LastName = extension.LastName,
         Email = appUser.Email,
         EmailConfirmed = appUser.EmailConfirmed,
         Level = appUser.Level,
         Credits = extension.Credits.ToString(),
         IndividualDescription = extension.IndividualDescription,
         PhoneNumber = extension.PhoneNumber,
         SkypeHandle = extension.SkypeHandle,
         JoinDate = appUser.JoinDate,
         Roles = _AppUserManager.GetRolesAsync(appUser.Id).Result,
         Claims = _AppUserManager.GetClaimsAsync(appUser.Id).Result
     };
 }
Ejemplo n.º 8
0
 public static async Task Static_SendNotification(Enum type, ApplicationUser user, List<string> users)
 {
     foreach (var appUser in users)
     {
         ConnectedUsers connectedUser = db.ConnectedUsers.FirstOrDefault(c => c.UserID == appUser);
         if (!Object.Equals(connectedUser, null))
             await hubContext.Clients.Client(connectedUser.ConnectionId).notificationReceived(user.UserName, type);
     }
 }
Ejemplo n.º 9
0
 public void SendNotification(Enum type, ApplicationUser user, List<ApplicationUser> users)
 {
     foreach (var appUser in users)
     {
         ConnectedUsers connectedUser = db.ConnectedUsers.FirstOrDefault(c => c.UserID == appUser.Id);
         if (!Object.Equals(connectedUser, null))
             Clients.Client(connectedUser.ConnectionId).notificationReceived(user.UserName, type);
      
     }
 }
Ejemplo n.º 10
0
        public async Task<IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel)
        {

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            bool IsMarketer = false;
            if (null != createUserModel.Marketer && !bool.TryParse(createUserModel.Marketer, out IsMarketer))
            {
                return BadRequest();
            }

            bool IsAffiliate = false;
            if (null != createUserModel.Affiliate && !bool.TryParse(createUserModel.Affiliate, out IsAffiliate))
            {
                return BadRequest();
            }

            if (!IsMarketer && !IsAffiliate)
            {
                return BadRequest("Affiliate or Marketer must be selected");
            }

            if (IsAffiliate)
            {
                string error = ValidateCreateModel(createUserModel, Role.Affiliate);
                if (!String.IsNullOrEmpty(error))
                {
                    return BadRequest(ModelState);
                }
            }

            // check if the user created a program
            if (IsMarketer)
            {
                string error = ValidateCreateModel(createUserModel, Role.Vendor);
                if (!String.IsNullOrEmpty(error))
                {
                    return BadRequest(ModelState);
                }
            }

            var user = new ApplicationUser()
            {
                UserName = createUserModel.Username,
                Email = createUserModel.Email,
                FirstName = createUserModel.FirstName,
                LastName = createUserModel.LastName,
                Level = 3,
                JoinDate = DateTime.Now.Date
            };

            IdentityResult addUserResult = await this.AppUserManager.CreateAsync(user, createUserModel.Password);

            if (!addUserResult.Succeeded)
            {
                return GetErrorResult(addUserResult);
            }

            // extend the user with specific information
            var userExt = new UserExtension()
            {
                SkypeHandle = createUserModel.SkypeHandle,
                UserId = user.Id,
                IndividualDescription = createUserModel.IndividualDescription,
                FirstName = createUserModel.FirstName,
                LastName = createUserModel.LastName,
                PhoneNumber = createUserModel.PhoneNumber,
                Category = Convert.ToInt32(createUserModel.UserCategory)
            };

            UserExtManager.UserExtensions.Add(userExt);

            try
            {
                int resultCount = await UserExtManager.Update();
            }
            catch (Exception ex)
            {
                // todo delete user here
                try
                {
                    await AppUserManager.DeleteAsync(user);
                }
                catch
                {
                    // do our best to not create secondary errors
                }

                return InternalServerError();
            }

            // check if the user created a program
            if (IsMarketer)
            {
                AppUserManager.AddToRole(user.Id, "Vendor");

                Program newProgram = new Program()
                {
                    CreatedDate = DateTime.Now,
                    CreatorId = user.Id,
                    Description = createUserModel.ProgramDescription,
                    Url = createUserModel.ProgramUrl,
                    Name = createUserModel.ProgramName,
                    Category = Convert.ToInt32(createUserModel.ProgramCategory)
                };

                MarketManager.Programs.Add(newProgram);
                await MarketManager.Update();
            }

            if (IsAffiliate)
            {
                AppUserManager.AddToRole(user.Id, "Affiliate");
            }

            Uri locationHeader = await SendConfirm(user);

            return Created(locationHeader, TheModelFactory.Create(user, userExt));
        }
Ejemplo n.º 11
0
        private async Task<Uri> SendConfirm(ApplicationUser user)
        {
            string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

            var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));

            await this.AppUserManager.SendEmailAsync(user.Id,
                                                    "Confirm your account",
                                                    "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

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

            return locationHeader;
        }