/*
        private ApplicationUserManager _userManager;
        public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }
        */
        public IHttpActionResult Post(RegisterViewModel user)
        {
            if (ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    if (!db.Users.Any(x => x.UserName == user.Username || x.Email == user.Email))
                    {

                            if(user.Password.Length<6) return BadRequest("The password must contain at least 6 characters");
                            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
                            ApplicationUser User = new ApplicationUser()
                            {
                                UserName = user.Username,
                                Email = user.Email,
                                FirstName = user.Name,
                                LastName = user.LastName,
                            };
                            manager.Create(User,user.Password);

                            try
                            {
                                MailMessage mailMsg = new MailMessage();

                                // To
                                mailMsg.To.Add(new MailAddress("*****@*****.**", "To Name"));

                                // From
                                mailMsg.From = new MailAddress("*****@*****.**", "From Name");

                                // Subject and multipart/alternative Body
                                mailMsg.Subject = "subject";
                                string text = "text body";
                                string html = @"<p>html body</p>";
                                mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
                                mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

                                // Init SmtpClient and send
                                SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));
                                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("*****@*****.**", "asdfasdfsadf323");
                                smtpClient.Credentials = credentials;

                                smtpClient.Send(mailMsg);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }

                        /* Catch ex when testing adding index to Firstname in ApplicationUser and user inputs an existing username
                            try
                            {
                                manager.Create(model, user.Password);
                            }
                            catch (Exception e)
                            {
                                return BadRequest("FirstName exists in db");
                                Console.WriteLine(e.StackTrace);
                            }
                         * */
                    }
                    else { return BadRequest("Username or Email already exists"); }
                }

                return Ok();
            }
            else
            {
                return BadRequest("Please include all required information.");
            }
        }
Exemplo n.º 2
0
        public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var info = await Authentication.GetExternalLoginInfoAsync();
            if (info == null)
            {
                return InternalServerError();
            }

            var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };

            IdentityResult result = await UserManager.CreateAsync(user);
            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            result = await UserManager.AddLoginAsync(user.Id, info.Login);
            if (!result.Succeeded)
            {
                return GetErrorResult(result); 
            }
            return Ok();
        }
Exemplo n.º 3
0
        private void SeedUsers(ApplicationDbContext db)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

            ApplicationUser user1 = new ApplicationUser()
            {
                UserName = "******",
                Email = "*****@*****.**",
                FirstName = "Doug",
                LastName = "Smith",
                Stars=5

            };

            ApplicationUser user2 = new ApplicationUser()
            {
                UserName = "******",
                Email = "*****@*****.**",
                FirstName = "Martha",
                LastName = "Jones",
                Stars = 5
            };

            ApplicationUser user3 = new ApplicationUser()
            {
                UserName = "******",
                Email = "*****@*****.**",
                FirstName = "Thomas",
                LastName = "Brown",
                Stars = 0
            };

            if (!db.Users.Any(x => x.UserName == "Doug")) { manager.Create(user1, "123123"); manager.AddToRole(user1.Id, "User"); }
            if (!db.Users.Any(x => x.UserName == "Martha")) { manager.Create(user2, "123123"); manager.AddToRole(user2.Id, "User"); }
            if (!db.Users.Any(x => x.UserName == "Thomas")) { manager.Create(user3, "123123"); manager.AddToRole(user3.Id, "User"); }

            Id1 = db.Users.FirstOrDefault(x => x.UserName == "Doug").Id; //Why does user1.Id throw an error?
            Id2 = db.Users.FirstOrDefault(x => x.UserName == "Martha").Id;
            Id3 = db.Users.FirstOrDefault(x => x.UserName == "Thomas").Id;
        }
Exemplo n.º 4
0
        public async Task<IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

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

            return Ok();
        }