Пример #1
0
 private Data.ApplicationUser GetApplicationUser(Facebook.Data.Account account, String accessToken)
 {
     Data.ApplicationUser user = new Data.ApplicationUser
     {
         Email       = account.Email,
         FacebookId  = Convert.ToInt64(account.Id),
         LastName    = account.LastName,
         UserName    = account.UserName,
         FirstName   = account.FirstName,
         AccessToken = accessToken,
         Gender      = account.Gender,
         PictureUrl  = account.PictureUrl
     };
     return(user);
 }
Пример #2
0
 private Core.Models.User GetTypeUser(Data.ApplicationUser currentUser)
 {
     Core.Models.User user = new Core.Models.User
     {
         Email       = currentUser.Email,
         Description = "",
         Firstname   = currentUser.FirstName,
         Lastname    = currentUser.LastName,
         Gender      = currentUser.Gender ?? "",
         Nickname    = currentUser.UserName ?? $"{currentUser.FirstName.ToLower()}.{currentUser.LastName.ToLower()}",
         Picture     = currentUser.PictureUrl ?? "",
         Phone       = "",
         Password    = "",
         Private     = false
     };
     return(user);
 }
Пример #3
0
 private async Task SignInAsync(ApplicationUser user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
 }
Пример #4
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new ApplicationUser() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Пример #5
0
        //[ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {

            //if (ModelState.IsValid)
            //{
            try
            {
                var user = new ApplicationUser() { UserName = model.UserName };
                string userrolename = "Users";
                if (!RoleManager.RoleExists(userrolename))
                {

                    var roleresult = RoleManager.Create(new IdentityRole(userrolename));

                }
                var uid = 0;
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    var result2 = UserManager.AddToRole(user.Id, userrolename);
                    if (user != null)
                    {
                        AspNetUsersAdditionalInfo objAspNetUsersAdditionalInfo = new AspNetUsersAdditionalInfo();
                        objAspNetUsersAdditionalInfo.UserKey = user.Id;
                        objAspNetUsersAdditionalInfo.CreatedOn = DateTime.Now;
                        objAspNetUsersAdditionalInfo.IsRemoved = false;
                        objAspNetUsersAdditionalInfo.CreatedFromIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
                        objAspNetUsersAdditionalInfo.SellerTypeID = Convert.ToInt32(this.RouteData.Values["Id"]);
                        _aspNetUsersAdditionalInfoService.InsertAspNetUsersAdditionalInfo(objAspNetUsersAdditionalInfo);
                        if (objAspNetUsersAdditionalInfo.ID > 0)
                        {
                            SellerPersonalInfo objSellerPersonalInfo = new SellerPersonalInfo();
                            objSellerPersonalInfo.UserID = objAspNetUsersAdditionalInfo.ID;
                            objSellerPersonalInfo.Title = model.Salutation.ToString();
                            objSellerPersonalInfo.FirstName = model.FirstName.ToString();
                            objSellerPersonalInfo.LastName = model.LastName.ToString();
                            objSellerPersonalInfo.Address = model.Address.ToString();
                            objSellerPersonalInfo.Email = model.Email.ToString();
                            objSellerPersonalInfo.ContactNo = model.Phone.ToString();
                            objSellerPersonalInfo.PostalCode = model.PostalCode.ToString();
                            _sellerPersonalInfoService.InsertSellerPersonalInfo(objSellerPersonalInfo);
                            if (objSellerPersonalInfo.ID > 0)
                            {
                                if (Convert.ToInt32(this.RouteData.Values["Id"]) == 2)
                                {
                                    SellerCompanyInfo objSellerCompanyInfo = new SellerCompanyInfo();
                                    objSellerCompanyInfo.UserID = objAspNetUsersAdditionalInfo.ID;
                                    objSellerCompanyInfo.CompanyTypeID = Convert.ToInt32(model.CompanyTypeID);
                                    objSellerCompanyInfo.CompanyName = model.CompanyName;
                                    objSellerCompanyInfo.Address001 = model.Address;
                                    objSellerCompanyInfo.CityID = 1;
                                    objSellerCompanyInfo.CountryID = Convert.ToInt32(model.CountryID);
                                    objSellerCompanyInfo.PostalCode = model.CompanyPostalCode;
                                    objSellerCompanyInfo.ContactNumbers = model.CompanyContact + "," + model.CompanyContact2 + "," + model.CompanyContact3;
                                    objSellerCompanyInfo.CompanyNumber = model.CompanyNumber;
                                    if (objSellerCompanyInfo.VatNumber != null)
                                        objSellerCompanyInfo.VatNumber = model.VATNumber;
                                    if (objSellerCompanyInfo.YearOfFoundation != null)
                                        objSellerCompanyInfo.YearOfFoundation = Convert.ToInt32(model.YearofFoundation);
                                    if (objSellerCompanyInfo.NoOfEmployees != null)
                                        objSellerCompanyInfo.NoOfEmployees = Convert.ToInt32(model.NoOfEmployee);
                                    if (objSellerCompanyInfo.TurnOver != null)
                                        objSellerCompanyInfo.TurnOver = (decimal)Decimal.Parse(model.TurnOver, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture);
                                    objSellerCompanyInfo.CategoryID = 1;
                                    _sellerCompanyInfoService.InsertSellerCompanyInfo(objSellerCompanyInfo);
                                    if (objSellerCompanyInfo.ID > 0)
                                    {
                                        if (!string.IsNullOrEmpty(model.CompanyLogo))
                                        {
                                            SellerCompanyLogo objSellerCompanyLogo = new SellerCompanyLogo();
                                            objSellerCompanyLogo.CompanyID = objSellerCompanyInfo.ID;
                                            objSellerCompanyLogo.Filename = model.CompanyLogo;
                                            objSellerCompanyLogo.FolderPath = "~/Content/Assets/CompanyLogos";
                                            objSellerCompanyLogo.OriginalFilename = model.CompanyLogo;
                                            objSellerCompanyLogo.UploadedOn = DateTime.Now;
                                            objSellerCompanyLogo.ImageSizeID = 1;
                                            _sellerCompanyLogoService.InsertSellerCompanyLogo(objSellerCompanyLogo);
                                        }
                                    }
                                }
                                await SignInAsync(user, isPersistent: false);
                                return RedirectToAction("Index", "Home");
                            }
                            else
                            {
                                ModelState.AddModelError("", "Registration is not done, please register after sometime later.");
                                return View();

                            }
                        }
                        else
                        {
                            ModelState.AddModelError("", "Registration is not done, please register after sometime later.");
                            return View();

                        }

                    }
                    else
                    {
                        ModelState.AddModelError("", "Registration is not done, please register after sometime later.");
                        return View();

                    }

                }
                else
                {
                    AddErrors(result);
                    
                    var countryList = _countryService.GetAllCountry();
                    if (countryList != null)
                    {
                        foreach (var item in countryList)
                        {
                            model.CountryList.Add(new Country
                            {
                                CountryName = item.CountryName,
                                ID = item.ID
                            });

                        }
                    }
                    var companyTypeList = _companyTypeService.GetAllCompanyType();
                    if (companyTypeList != null)
                    {
                        foreach (var item in companyTypeList)
                        {
                            model.CompanyTypeList.Add(new SelectListItem
                            {
                                Text = item.CompanyType1,
                                Value = item.ID.ToString()
                            });
                        }
                    }
                

                }
               
            }
            catch (Exception ex)
            { }
            return View(model);
        }
        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();
        }
        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();
        }
        private static void SeedUsers(ApplicationDbContext context)
        {
            var usersCount = 50;
            if (!context.Roles.Any(r => r.Name == "Admin"))
            {
                var store = new RoleStore<IdentityRole>(context);
                var manager = new RoleManager<IdentityRole>(store);
                var role = new IdentityRole { Name = "Admin" };

                manager.Create(role);
            }

            if (!context.Users.Any(u => u.UserName == "*****@*****.**"))
            {
                var store = new UserStore<ApplicationUser>(context);
                var manager = new ApplicationUserManager(store);
                var user = new ApplicationUser
                {
                    Email = "*****@*****.**",
                    UserName = "******",
                    PasswordHash = new PasswordHasher().HashPassword("admin"),
                    SecurityStamp = Guid.NewGuid().ToString(),
                    FirstName = "Pe6o admina",
                    LastName = "Adminov",
                    GSM = "99 999 999"
                };

                context.Users.AddOrUpdate(user);
                context.SaveChanges();
                manager.AddToRole(user.Id, "Admin");
            }

            if (context.Users.Count() <= 1)
            {
                var random = new Random();
                for (int i = 1; i <= usersCount; i++)
                {
                    var user = new ApplicationUser()
                    {
                        UserName = $"u{i}@site.com",
                        Email = $"u{i}@site.com",
                        PasswordHash = new PasswordHasher().HashPassword($"u{i}"),
                        SecurityStamp = Guid.NewGuid().ToString(),
                        FirstName = Names[random.Next(0, Names.Length)],
                        LastName = Names[random.Next(0, Names.Length)],
                        GSM = random.Next(100000000, 999999999).ToString()
                    };

                    context.Users.AddOrUpdate(user);
                }

                context.SaveChanges();
            }
        }