コード例 #1
0
        public async Task <IActionResult> Login(string returnUrl)
        {
            ViewLoginModel model = new ViewLoginModel()
            {
                ReturnUrl      = returnUrl,
                ExternalLogins = (await signInManager.GetExternalAuthenticationSchemesAsync()).ToList()
            };

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> ExternalLoginCallBack(string returnUrl = null, string remoteError = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            ViewLoginModel viewLoginModel = new ViewLoginModel
            {
                ReturnUrl      = returnUrl,
                ExternalLogins = (await signInManager.GetExternalAuthenticationSchemesAsync()).ToList()
            };

            if (remoteError != null)
            {
                ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}");

                return(View("Login", viewLoginModel));
            }

            var info = await signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ModelState.AddModelError(string.Empty, "Error login external login information.");

                return(View("Login", viewLoginModel));
            }

            var signInResult = await signInManager.ExternalLoginSignInAsync(info.LoginProvider,
                                                                            info.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            if (signInResult.Succeeded)
            {
                return(LocalRedirect("~/Home/start"));
            }
            else
            {
                var email = info.Principal.FindFirstValue(ClaimTypes.Email);

                if (email != null)
                {
                    var user = await userManager.FindByEmailAsync(email);

                    await userManager.AddLoginAsync(user, info);

                    await signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }
            }
            ViewBag.ErrorTitle = $"Email calin not received from: {info.LoginProvider}";

            return(View("Error"));
        }
コード例 #3
0
        public ActionResult Login(ViewLoginModel model, string path)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            else
            {
                if (string.IsNullOrEmpty(path))
                {
                    return(Content("路径不对"));
                }

                var loginPath = ConfigurationManager.AppSettings["LoginPath"];
                if (!path.Equals(loginPath))
                {
                    return(Content("路径不对"));
                }
                //从配置文件读取账号和MD5加密的密码
                var adminName     = ConfigurationManager.AppSettings["AdminName"];
                var adminPassword = ConfigurationManager.AppSettings["AdminPassword"];
                if (adminName.Equals(model.Name) && adminPassword.Equals(GetMD5(model.Password)))
                {
                    //写登录票据
                    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                        // 版本号
                        1,
                        // 与身份验证票关联的用户名
                          adminName,
                        // cookie 的发出时间
                          DateTime.Now,
                          // cookie 的到期日期
                        DateTime.Now.Add(FormsAuthentication.Timeout),
                        // 如果 cookie 是持久化的,为 true;否则为 false。
                          true,
                        // 将存储在 cookie 中的用户定义数据。roles是一个角色字符串数组
                                              "administrator");
                    //加密
                    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

                    //存入cookie
                    HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    Response.Cookies.Add(authCookie);
                    return(Redirect("/"));
                }
                else
                {
                    return(View("~/Views/Shared/Error.cshtml"));
                }
            }
            return(View(model));
        }
コード例 #4
0
ファイル: LoginController.cs プロジェクト: lovachen/FastCore
        public IActionResult DoLogin(ViewLoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return(NotValid());
            }
            var res = _sysUserService.ValidateUser(model.Account, model.Password, 0);

            AjaxData.Message = res.Message;
            AjaxData.Code    = res.Status ? 0 : 2001;
            if (res.Status)
            {
                _sysUserAuthentication.SignIn(res.Jwt.Jti, res.User, res.Jwt.Expiration);
                AjaxData.Result = new { Url = Url.RouteUrl("mainIndex") };
            }
            return(Json(AjaxData));
        }
コード例 #5
0
        public ActionResult Login(ViewLoginModel userModel)
        {
            using (Internship_Section1Entities DB = new Internship_Section1Entities())
            {
                if (ModelState.IsValid)
                {
                    var user = (from list in DB.UserDetails
                                where list.EmailAddress == userModel.EmailAddress
                                select new
                    {
                        list.UserID,
                        list.EmailAddress,
                        list.Position
                    }).ToList();
                    if (user.FirstOrDefault() != null)
                    {
                        Session["Email"]     = user.FirstOrDefault().EmailAddress;
                        Session["StudentID"] = user.FirstOrDefault().UserID;
                        Session["Position"]  = user.FirstOrDefault().Position;


                        if (Session["Position"].ToString() == "Student")            //ONLY the Admin Role
                        {
                            return(RedirectToAction("ProfileDetails", "Students")); //The Views they can see
                        }
                        if (Session["Position"].ToString() == "Lecturer")           //ONLY the Moderator Role
                        {
                            return(RedirectToAction("Index", "Lecturer"));          //The Views they can see
                        }
                    }
                    else
                    {
                        //When the user entered the wrong login details this message will appear.
                        ModelState.AddModelError("", "Invalid login credentials.");
                        return(View()); //The login view will reappear.
                    }
                }
                return(View(userModel));
            }
        }
コード例 #6
0
        public async Task <IActionResult> Login([FromForm] ViewLoginModel model)
        {
            var dbModel = _mapper.Map <LoginModel>(model);
            var result  = _manager.Login(dbModel);

            if (result == null)
            {
                return(BadRequest());
            }

            var claims = new List <Claim>()
            {
                new Claim(ClaimTypes.Name, result.FirstName),
                new Claim(ClaimTypes.Surname, result.LastName)
            };
            ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id));

            await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);

            return(Ok(result));
        }
コード例 #7
0
        public async Task <IActionResult> Login(ViewLoginModel reg, string returnUrl)
        {
            reg.ReturnUrl      = returnUrl;
            reg.ExternalLogins = (await signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var result = await signInManager.PasswordSignInAsync(reg.Email, reg.Password, reg.RememberMe, false);

                if (result.Succeeded)
                {
                    if (!string.IsNullOrEmpty(returnUrl) /*&& Url.IsLocalUrl(returnUrl)*/) //1-rd zev
                    {
                        return(LocalRedirect(returnUrl));                                  //2-rd zev
                    }
                    else
                    {
                        return(RedirectToAction("start", "home"));
                    }
                }
                ModelState.AddModelError(string.Empty, "Invalid Login Attempt");
            }

            return(View(reg));
        }