Ejemplo n.º 1
0
        public int AddUserRole(string Username, string Role, bool Change)
        {
            try
            {
                var id = (from m in db.MstUsers
                          join n in db.AspNetUsers on m.AspNetUserId equals n.Id
                          where m.UserName == Username
                          select new Models.User
                {
                    AspNetUserId = m.AspNetUserId
                }).ToList();

                var AspNetUserId = id[0].AspNetUserId;

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

                NewRole.UserId = AspNetUserId;
                NewRole.RoleId = Role;

                if (Change)
                {
                    db.AspNetUserRoles.InsertOnSubmit(NewRole);
                }
                try
                {
                    db.SubmitChanges();
                    return(1);
                }
                catch
                {
                    return(0);
                }
            }
            catch
            {
                return(0);
            }
        }
Ejemplo n.º 2
0
        public HttpResponseMessage Delete(string Username, string Role)
        {
            var UserId = getAspNetUserId(Username);

            Data.AspNetUserRole DeleteRole = db.AspNetUserRoles.Where(d => d.RoleId == Role && d.UserId == UserId).First();

            if (DeleteRole != null)
            {
                db.AspNetUserRoles.DeleteOnSubmit(DeleteRole);
                try
                {
                    db.SubmitChanges();
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                catch
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest));
                }
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
0
        public HttpResponseMessage Put(String Id, Models.User value)
        {
            Id = Id.Replace(",", "");
            int id = Convert.ToInt32(Id);

            bool   withSMS      = false;
            String aspNetUserId = "";

            try
            {
                var Users = from d in db.MstUsers where d.Id == id select d;

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

                    withSMS = value.WithSMS == null ? false : value.WithSMS;

                    UpdatedUser.UserName     = value.UserName;
                    UpdatedUser.FirstName    = value.FirstName;
                    UpdatedUser.LastName     = value.LastName;
                    UpdatedUser.EmailAddress = value.EmailAddress;
                    UpdatedUser.PhoneNumber  = value.PhoneNumber;
                    UpdatedUser.Address      = value.Address;
                    UpdatedUser.WithSMS      = withSMS;

                    db.SubmitChanges();

                    aspNetUserId = UpdatedUser.AspNetUserId;

                    var roles = from d in db.AspNetUserRoles
                                where d.UserId == aspNetUserId &&
                                d.RoleId == db.AspNetRoles.Where(r => r.Name == "SMS").FirstOrDefault().Id
                                select d;

                    if (withSMS == true)
                    {
                        if (!roles.Any())
                        {
                            Data.AspNetUserRole NewRole = new Data.AspNetUserRole();

                            NewRole.UserId     = aspNetUserId;
                            NewRole.AspNetRole = db.AspNetRoles.Where(d => d.Name == "SMS").FirstOrDefault();

                            db.AspNetUserRoles.InsertOnSubmit(NewRole);
                            db.SubmitChanges();
                        }
                    }
                    else
                    {
                        if (roles.Any())
                        {
                            Data.AspNetUserRole DeleteRole = roles.First();
                            db.AspNetUserRoles.DeleteOnSubmit(DeleteRole);
                            db.SubmitChanges();
                        }
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (NullReferenceException)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }