Esempio n. 1
0
        public IActionResult signup(UserViewModel model)
        {
            try
            {
                ModelState.Remove("Address");
                ModelState.Remove("City");
                ModelState.Remove("ZipCode");
                if (ModelState.IsValid)
                {
                    using (var DB = _dbContext)
                    {
                        TblAccount oData = new TblAccount();
                        if (!IsCompanyExist(model.EmailAddress))
                        {
                            oData.FullName    = string.IsNullOrEmpty(model.FullName) ? string.Empty : model.FullName;
                            oData.PhoneNumber = string.IsNullOrEmpty(model.CellPhone) ? string.Empty : model.CellPhone;
                            oData.UserName    = string.IsNullOrEmpty(model.EmailAddress) ? string.Empty : model.EmailAddress;
                            oData.Password    = string.IsNullOrEmpty(model.Password) ? string.Empty : Encryption.EncryptText(model.Password);
                            oData.IsOwner     = true;
                            oData.CreatedDate = DateTime.Now;
                            oData.RoleId      = RoleType.Admin.GetHashCode();
                            oData.Status      = true;
                            DB.TblAccounts.Add(oData);
                            DB.SaveChanges();


                            TblAccountCompany oDataCompany = new TblAccountCompany();
                            oDataCompany.AccountId   = oData.AccountId;
                            oDataCompany.CreatedDate = DateTime.Now;
                            DB.TblAccountCompanies.Add(oDataCompany);
                            DB.SaveChanges();


                            SetCookie("EmailAddress", oData.UserName);
                            SetCookie("FullName", oData.FullName);
                            SetCookie("LoginAccountId", oData.AccountId.ToString());
                            SetCookie("UserLoginTypeId", RoleType.Admin.GetHashCode().ToString());
                            //SetCookie("UserLoginTypeId", UserLoginType.Company.GetHashCode().ToString());
                            return(RedirectToAction("Index", "Lead", new { area = "" }));
                        }
                        else
                        {
                            ShowWarningMessage("EmailAddress already taken.", true);
                            return(View(model));
                        }
                    }
                }
                else
                {
                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                string actionName     = this.ControllerContext.RouteData.Values["action"].ToString();
                string controllerName = this.ControllerContext.RouteData.Values["controller"].ToString();
                ErrorLog.logError(DateTime.Now + "--" + actionName + "--" + controllerName + "--\n" + ex, Environment.WebRootPath);
                return(RedirectToAction("Login", "Account"));
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> MicrosoftResponse()
        {
            try
            {
                // Here the following code reperesent that user is sucessfully authenticated by microsoft
                var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);

                if (result.Principal.Claims.Count() <= 0)
                {
                    throw new Exception("User is not authorize!");
                }
                // Here the following code reperesent that user is sucessfully authenticated by google.
                //We are taking user's information and redirecting user to his environment.
                TblAccount oUser = new TblAccount();
                oUser.UserName = result.Principal.FindFirst(ClaimTypes.Email).Value;
                oUser.FullName = result.Principal.FindFirst(ClaimTypes.Name).Value;
                oUser.RoleId   = RoleType.Admin.GetHashCode();
                oUser.IsOwner  = true;
                oUser.Status   = true;

                if (!IsCompanyExist(oUser.UserName))
                {
                    oUser.CreatedDate = DateTime.Now;
                    _dbContext.TblAccounts.Add(oUser);
                    _dbContext.SaveChanges();

                    TblAccountCompany oDataCompany = new TblAccountCompany();
                    oDataCompany.AccountId   = oUser.AccountId;
                    oDataCompany.AddedBy     = oUser.AccountId;
                    oDataCompany.CreatedDate = DateTime.Now;
                    _dbContext.TblAccountCompanies.Add(oDataCompany);
                    _dbContext.SaveChanges();
                }
                else
                {
                    oUser = _dbContext.TblAccounts.Where(x => x.UserName.Equals(oUser.UserName)).FirstOrDefault();
                }

                if (string.IsNullOrEmpty(oUser.Password))
                {
                    TempData["EmailAddress"] = oUser.UserName;
                    TempData.Keep();
                    return(RedirectToAction("SetPassword", "Company"));
                }
                else
                {
                    //Here we are storing claims for authentication
                    ClaimsIdentity identity = null;
                    identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, oUser.UserName), new Claim(ClaimTypes.Role, "Company") }, CookieAuthenticationDefaults.AuthenticationScheme);
                    var prinicpal = new ClaimsPrincipal(identity);
                    var login     = HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, prinicpal);
                    //Redirection of lead user is here. Please give appropreate direction URL to it


                    SetCookie("EmailAddress", oUser.UserName);
                    SetCookie("FullName", oUser.FullName);
                    SetCookie("LoginAccountId", oUser.AccountId.ToString());
                    SetCookie("UserLoginTypeId", RoleType.Admin.GetHashCode().ToString());
                    //SetCookie("EmailAddress", oCompany.Email);
                    //SetCookie("FullName", oCompany.FullName);
                    //SetCookie("LoginAccountId", oCompany.CompanyId.ToString());
                    //SetCookie("UserLoginTypeId", UserLoginType.Company.GetHashCode().ToString());
                    return(RedirectToAction("Index", "Lead"));
                }
            }
            catch (Exception ex)
            {
                string actionName     = this.ControllerContext.RouteData.Values["action"].ToString();
                string controllerName = this.ControllerContext.RouteData.Values["controller"].ToString();
                ErrorLog.logError(DateTime.Now + "--" + actionName + "--" + controllerName + "--\n" + ex, Environment.WebRootPath);
                return(RedirectToAction("Login", "Account"));
            }
        }