Beispiel #1
0
        //[ValidateAntiForgeryToken]
        public async Task <ActionResult> Register(RegisterPageWraper model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.RegisterViewModel.Email, Email = model.RegisterViewModel.Email
                };
                var result = await UserManager.CreateAsync(user, model.RegisterViewModel.Password);

                if (result.Succeeded)
                {
                    // mobile number
                    var mobile = model.RegisterViewModel.Mobile;
                    mobile = mobile.Substring(1).Replace("-", "");
                    model.RegisterViewModel.Mobile = "92" + mobile;

                    UserServices.AddUserProperties(model.RegisterViewModel, user.Id);
                    var uObj = UserServices.GetUserById(user.Id);
                    SetUserSession(uObj, true);

                    var content = "Hi " + model.RegisterViewModel.FullName +
                                  "!\nYou have been successfully registered as a " + model.RegisterViewModel.Type +
                                  " at KamSham.Pk.";
                    if (model.RegisterViewModel.Type == "Contractor")
                    {
                        content = content + "You will be able to login once we approve your account information.";
                    }
                    content = content + "\n-KamSham Team\n+923084449991";
                    KaamShaam.Services.EmailService.SendEmail(user.Email, "Registration Notification | KamSham.Pk", content);

                    TempData.Add("userId", user.Id);
                    TempData.Add("userNumber", model.RegisterViewModel.Mobile);

                    return(RedirectToAction("VerifyNumber", "Account"));

                    //if (model.RegisterViewModel.Type == "Contractor")
                    //{
                    //    return RedirectToAction("Welcome", "Account");
                    //}

                    //   await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    //   return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }
            var cats = CategoryService.GetAllCategories();

            model.Categories = cats;
            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #2
0
        public async Task <HttpResponseMessage> RegisterUser(RegisterViewModel model)
        {
            try
            {
                #region Validation
                HttpResponseMessage endResponse;
                var response = new ApiResponseModel {
                    Data = model
                };
                if (model == null || !ModelState.IsValid)
                {
                    response.Message = "Mandatory data is missing. ";
                    foreach (var error in ModelState)
                    {
                        response.Message = response.Message + "\n" + error.Key;
                    }
                    response.Success = false;
                    endResponse      = Request.CreateResponse(HttpStatusCode.BadRequest, response);
                    return(endResponse);
                }
                if (model.Type == "Contractor" && string.IsNullOrEmpty(model.CNIC))
                {
                    response.Message = "CNIC is missing. ";
                    response.Success = false;
                    endResponse      = Request.CreateResponse(HttpStatusCode.BadRequest, response);
                    return(endResponse);
                }
                if (model.Type == "Contractor" && model.CategoryId == 0)
                {
                    response.Message = "Category is missing for contractor ";
                    response.Success = false;
                    endResponse      = Request.CreateResponse(HttpStatusCode.BadRequest, response);
                    return(endResponse);
                }
                if (model.LocationCord == null || string.IsNullOrEmpty(model.LocationName))
                {
                    response.Message = "LocationCord(lat_lng)/LocationName is missing ";
                    response.Success = false;
                    endResponse      = Request.CreateResponse(HttpStatusCode.BadRequest, response);
                    return(endResponse);
                }
                #endregion
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var usermanager = Request.GetOwinContext().GetUserManager <ApplicationUserManager>();
                var result      = await usermanager.CreateAsync(user, model.Password);

                var isUserCreated = result.Succeeded;
                if (isUserCreated)
                {
                    try
                    {
                        response.Message = model.FullName + " has been registered";
                        response.Success = true;
                        UserServices.AddUserProperties(model, user.Id);
                        var baseUrlforimg = System.Web.Hosting.HostingEnvironment.MapPath("/Images/Profiles/");
                        UserServices.CreateUserAvatar(baseUrlforimg + user.Id);
                        response.Data = user;

                        var content = "Hi " + model.FullName +
                                      "!\nYou have been successfully registered as a " + model.Type +
                                      " at KamSham.Pk.";
                        if (model.Type == "Contractor")
                        {
                            content = content + "You will be able to login once we approve your account information.";
                        }
                        content = content + "\n-KamSham Team\n+923084449991";
                        KaamShaam.Services.EmailService.SendEmail(user.Email, "Registration Notification | KamSham.Pk", content);
                    }
                    catch (Exception excep)
                    {
                        var foundUser    = usermanager.FindById(user.Id);
                        var responseMesg = await usermanager.DeleteAsync(user);

                        if (responseMesg.Succeeded)
                        {
                            // user is deleted
                        }
                        response.Message = "Data is missing." + excep.InnerException.Message;
                        response.Success = false;
                    }
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        response.Message = response.Message + "\n" + error;
                    }
                    response.Success = false;
                }
                endResponse = Request.CreateResponse(HttpStatusCode.OK, response);
                return(endResponse);
            }
            catch (Exception excep)
            {
                var response = new ApiResponseModel
                {
                    Data    = model,
                    Message = excep.InnerException.Message,
                    Success = false
                };
                var endResponse = Request.CreateResponse(HttpStatusCode.InternalServerError, response);
                return(endResponse);
            }
        }