public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    UserName = model.UserName
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent : false);

                    // Add or update MstUser table
                    Data.MagentaTradersDBDataContext db = new Data.MagentaTradersDBDataContext();

                    var Users = from d in db.MstUsers where d.UserName == model.UserName select d;

                    if (Users.Any())
                    {
                        var UpdatedUser = Users.FirstOrDefault();

                        UpdatedUser.AspNetUserId = db.AspNetUsers.Where(d => d.UserName == model.UserName).FirstOrDefault().Id;

                        db.SubmitChanges();
                    }
                    else
                    {
                        Data.MstUser NewUser = new Data.MstUser();

                        NewUser.UserName     = model.UserName;
                        NewUser.FirstName    = "NA";
                        NewUser.LastName     = "NA";
                        NewUser.EmailAddress = "NA";
                        NewUser.PhoneNumber  = "NA";
                        NewUser.AspNetUserId = db.AspNetUsers.Where(d => d.UserName == model.UserName).FirstOrDefault().Id;

                        db.MstUsers.InsertOnSubmit(NewUser);
                        db.SubmitChanges();
                    }

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (model.UserName == null)
            {
                return(View(model));
            }

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    UserName = model.UserName
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                var    response  = HttpContext.Request.Form["g-recaptcha-response"] == null ? "" : HttpContext.Request.Form["g-recaptcha-response"];
                string secretKey = "6Lc5GBoTAAAAAOQFNfUBzRtzN_I-vmyJzGugEx65";
                var    client    = new System.Net.WebClient();
                var    verificationResultJson = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secretKey, response));
                var    verificationResult     = JsonConvert.DeserializeObject <CaptchaVerificationResult>(verificationResultJson);

                if (!verificationResult.Success)
                {
                    ModelState.AddModelError("", "ERROR: Invalid recaptcha challenge.");
                }
                else
                {
                    if (result.Succeeded)
                    {
                        // Add or update MstUser table
                        try
                        {
                            await SignInAsync(user, isPersistent : false);

                            Data.MagentaTradersDBDataContext db = new Data.MagentaTradersDBDataContext();

                            var Users = from d in db.MstUsers where d.UserName == model.UserName select d;

                            if (Users.Any())
                            {
                                var UpdatedUser = Users.FirstOrDefault();

                                UpdatedUser.AspNetUserId = db.AspNetUsers.Where(d => d.UserName == model.UserName).FirstOrDefault().Id;

                                db.SubmitChanges();
                            }
                            else
                            {
                                Data.MstUser NewUser = new Data.MstUser();

                                NewUser.UserName         = model.UserName;
                                NewUser.FirstName        = model.FirstName == null || model.FirstName.Length == 0 ? "NA" : model.FirstName;
                                NewUser.LastName         = model.LastName == null || model.LastName.Length == 0 ? "NA" : model.LastName;
                                NewUser.EmailAddress     = model.EmailAddress == null || model.EmailAddress.Length == 0 ? "NA" : model.EmailAddress;
                                NewUser.PhoneNumber      = model.PhoneNumber == null || model.PhoneNumber.Length == 0 ? "NA" : model.PhoneNumber;
                                NewUser.Address          = model.Address == null || model.Address.Length == 0 ? "" : model.Address;
                                NewUser.ReferralUserName = model.ReferralUserName == null || model.ReferralUserName.Length == 0 ? "" : model.ReferralUserName;
                                NewUser.AspNetUserId     = db.AspNetUsers.Where(d => d.UserName == model.UserName).FirstOrDefault().Id;

                                DateTime    dateCreated    = DateTime.Now;
                                SqlDateTime dateCreatedSQL = new SqlDateTime(new DateTime(dateCreated.Year, +
                                                                                          dateCreated.Month, +
                                                                                          dateCreated.Day));
                                NewUser.DateCreated = dateCreatedSQL.Value;

                                db.MstUsers.InsertOnSubmit(NewUser);
                                db.SubmitChanges();

                                Data.AspNetUserRole NewRole1 = new Data.AspNetUserRole();

                                NewRole1.AspNetUser = db.AspNetUsers.Where(d => d.UserName == model.UserName).FirstOrDefault();
                                NewRole1.AspNetRole = db.AspNetRoles.Where(d => d.Name == "Quest").FirstOrDefault();

                                db.AspNetUserRoles.InsertOnSubmit(NewRole1);
                                db.SubmitChanges();

                                Data.AspNetUserRole NewRole2 = new Data.AspNetUserRole();

                                NewRole2.AspNetUser = db.AspNetUsers.Where(d => d.UserName == model.UserName).FirstOrDefault();
                                NewRole2.AspNetRole = db.AspNetRoles.Where(d => d.Name == "Chart").FirstOrDefault();

                                db.AspNetUserRoles.InsertOnSubmit(NewRole2);
                                db.SubmitChanges();

                                Data.AspNetUserRole NewRole3 = new Data.AspNetUserRole();

                                NewRole3.AspNetUser = db.AspNetUsers.Where(d => d.UserName == model.UserName).FirstOrDefault();
                                NewRole3.AspNetRole = db.AspNetRoles.Where(d => d.Name == "Web99").FirstOrDefault();

                                db.AspNetUserRoles.InsertOnSubmit(NewRole3);
                                db.SubmitChanges();
                            }
                            return(RedirectToAction("Index", "Help"));
                            //return RedirectToAction("Index", "Home");
                        }
                        catch (Exception e)
                        {
                            ModelState.AddModelError("", "ERROR: Try again. " + e.ToString());
                        }
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }