public async Task <IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var user = new ApplicationUser()
            {
                UserName = createUserModel.Username,
                Email    = createUserModel.Email,
                JoinDate = DateTime.Now.Date,
            };


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

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

            var userEntity    = this.AppUserManager.Users.FirstOrDefault(x => x.Email == createUserModel.Email);
            var newEmployeeId = _employeeService.Post(new EmployeeDto()
            {
                CompanyId = 1,
                Id        = createUserModel.DeveloperId,
                Email     = createUserModel.Email,
                FirstName = createUserModel.FirstName,
                Surname   = createUserModel.LastName,
            }, userEntity);

            var readClaim         = ExtendedClaimsProvider.CreateClaim("canReadUsers", "true");
            var readProjectsClaim = ExtendedClaimsProvider.CreateClaim("canReadProjects", "true");

            AppUserManager.AddClaim(userEntity.Id, readClaim);
            AppUserManager.AddClaim(userEntity.Id, readProjectsClaim);

            string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

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

            try
            {
                await this.AppUserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking: " + callbackUrl);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

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

            return(Created(locationHeader, TheModelFactory.Create(user)));
        }
コード例 #2
0
        // POST api/Account/Register
        /// <summary>
        /// JB. Asynhronous task to Register a user by providing Emaiil address ad password. (User must enter password twice to confirm)
        /// </summary>
        /// <param name="model">RegisterBindingModel</param>
        /// <returns></returns>
        public async Task <IHttpActionResult> Register(CreateUserBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //JB. if user has not send username, then set the Email as username.
            var user = new ApplicationUser()
            {
                UserName = model.Username = String.IsNullOrEmpty(model.Username) ? model.Email : model.Username, Email = model.Email
            };
            IdentityResult result = await AppUserManager.CreateAsync(user, model.Password);

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



            string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

            //JB.Build Email callback where user will Confirm Email (When the user is interacting without a Wrapper).
            var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code, email = user.Email }));

            await this.AppUserManager.SendEmailAsync(user.Id,
                                                     "Please Confirm your Email",
                                                     "<img src=\"" + System.Configuration.ConfigurationManager.AppSettings["BaseUrlAddress"] + "Content/Images/header.png" + "\"><br/> Welcome to The Channel, in order to continue with your registration Please confirm your email address by clicking <a href=\"" + callbackUrl + "\">here</a>");

            //JB. Once confirmed, tell our app and update AspNet users table accordingly ;)
            Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));

            AppUserManager.AddClaim(user.Id, new Claim(ClaimTypes.Uri, user.Email));
            //AppUserManager.AddToRole(user.Id, "User");
            var dis = new Dictionary <string, Uri>();

            dis.Add("userUrl", locationHeader);

            var daReturnedUSer = JsonConvert.SerializeObject(dis);

            //JB. Return generated UserId to client
            return(Ok(daReturnedUSer));
        }