コード例 #1
0
        /// <summary>
        /// 具体判断方法
        /// </summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        protected bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            string ticket = httpContext.Request["Ticket"];

            if (!string.IsNullOrEmpty(ticket))
            {
                //如果认证服务器返回票据,则记录
                User user = new User
                {
                    Name = "Client1"
                };
                FormsAuthenticationTicket authenticationTicket = FormsAuthenticationHelper.CreateAuthenticationTicket(user);
                FormsAuthenticationHelper.SetAuthCookie(httpContext, authenticationTicket);
                return(true);
            }
            FormsIdentity formsIdentity = httpContext.User.Identity as FormsIdentity;

            //验证cookie 用户是否有效
            if (formsIdentity == null)
            {
                return(false);
            }
            //这里可以做授权验证
            //....
            return(true);
        }
コード例 #2
0
 public void SignIn(RestaurantUser user, bool createPersistentCookie)
 {
     if (user == null)
     {
         throw new ArgumentNullException("user", "Value cannot be null or empty");
     }
     FormsAuthenticationHelper.SetAuthCookie(user, createPersistentCookie);
 }
コード例 #3
0
        public ActionResult LoginAsOther(string user)
        {
            if (!Request.IsLocal)
            {
                return(Content("Invalid"));
            }
            RequestResult <string[]> result = GetAccessableBrands(user);

            if (!string.IsNullOrEmpty(result.ErrorMessage))
            {
                ModelState.AddModelError("", result.ErrorMessage);
                return(View(user));
            }
            FormsAuthenticationHelper.SetAuthCookie(user, false, string.Join(",", result.ReturnValue));
            return(this.CloseModalView());
        }
コード例 #4
0
 public ActionResult Login(string userName, string password)
 {
     if ((userName == "qxh" && password == "123") || (userName == "jlp" && password == "123"))
     {
         //如果认证服务器返回票据,则记录
         User user = new User
         {
             Name = "SSOServer"
         };
         FormsAuthenticationTicket authenticationTicket = FormsAuthenticationHelper.CreateAuthenticationTicket(user);
         FormsAuthenticationHelper.SetAuthCookie(base.HttpContext, authenticationTicket);
         string ReturnURL = Request["ReturnURL"] + "?Ticket=SSOServer";
         return(Redirect(ReturnURL));
     }
     return(View());
 }
コード例 #5
0
 public ActionResult LogOn(string token)
 {
     if (!string.IsNullOrWhiteSpace(token))
     {
         LoginProfile item = LoginProfile.Parse(token);
         if (item != null)
         {
             LoginApiClient login = new LoginApiClient();
             using (login.Wrapper)
             {
                 UserProfile profile = login.UserProfile(item.Username).ReturnValue?.data;
                 if (item.Username.EqualsIgnoreCaseAndBlank("admin") || profile != null && profile.Authority?.Any(p => p.EqualsIgnoreCaseAndBlank(item.Country)) == true)
                 {
                     CmdResult res = UpdateUsername(item.Username, profile?.UserName).Result;
                     RequestResult <string[]> result = GetAccessableBrands(item.Username);
                     if (!string.IsNullOrWhiteSpace(result.ErrorMessage))
                     {
                         ModelState.AddModelError("", result.ErrorMessage);
                     }
                     else
                     {
                         FormsAuthenticationHelper.SetAuthCookie(item.Username.Trim(), false, string.Join(",", result.ReturnValue));
                         return(RedirectToAction("Index", "Home", new { lang = item.Lang }));
                     }
                 }
                 else
                 {
                     ModelState.AddModelError("", $"You are not allowed to visit {item.Country}'s intranet");
                 }
             }
         }
         else
         {
             ModelState.AddModelError("", StringResource.INVALID_USERNAME_OR_PASSWORD);
         }
     }
     Response.Buffer          = true;
     Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
     Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
     Response.Expires      = 0;
     Response.CacheControl = "no-cache";
     Response.Cache.SetNoStore();
     return(View());
 }
コード例 #6
0
        public async Task <ActionResult> LogOn(LogOnViewModel user)
        {
            if (ModelState.IsValid)
            {
                UserLoginProfile profile = await LoginManager.Authenticate(user, HttpContext.IsDebuggingEnabled);

                if (profile != null)
                {
                    bool confirm = false;
                    if (!string.IsNullOrEmpty(profile.error))
                    {
                        if (profile.error.IndexOf("패스워드가 만료 되었습니다", StringComparison.InvariantCultureIgnoreCase) >= 0 ||
                            profile.error.IndexOf("密碼已經過期", StringComparison.InvariantCultureIgnoreCase) >= 0 ||
                            profile.error.IndexOf("password has been expired", StringComparison.InvariantCultureIgnoreCase) >= 0 ||
                            profile.error.IndexOf("密码已经过期", StringComparison.InvariantCultureIgnoreCase) >= 0)
                        {
                            return(View("ChangePassword", (object)user.Username));
                        }

                        if (profile.error.IndexOf("密碼將於", StringComparison.InvariantCultureIgnoreCase) >= 0 &&
                            profile.error.IndexOf("天後到期", StringComparison.InvariantCultureIgnoreCase) >= 0 ||
                            profile.error.IndexOf("days left to be password expiration",
                                                  StringComparison.InvariantCultureIgnoreCase) >= 0 ||
                            profile.error.IndexOf("패스워드 만료가", StringComparison.InvariantCultureIgnoreCase) >= 0 &&
                            profile.error.IndexOf("일 남았습니다", StringComparison.InvariantCultureIgnoreCase) >= 0)
                        {
                            confirm = true;
                        }

                        if (!confirm)
                        {
                            ModelState.AddModelError("", profile.error);
                            return(View(user));
                        }
                    }
                    await UpdateUsername(user.Username, profile.UserName);

                    RequestResult <string[]> result = GetAccessableBrands(user.Username);
                    if (!string.IsNullOrEmpty(result.ErrorMessage))
                    {
                        ModelState.AddModelError("", result.ErrorMessage);
                        return(View(user));
                    }
                    string lang = Codehelper.GetLang(profile.Language);
                    if (HttpContext.IsDebuggingEnabled)
                    {
                        FormsAuthenticationHelper.SetAuthCookie(user.Username, false, string.Join(",", result.ReturnValue));
                        return(RedirectToAction("Index", "Home", new { lang }));
                    }
                    FormsAuthenticationHelper.SetAuthCookie(user.Username, false, string.Join(",", result.ReturnValue));
                    if (confirm)
                    {
                        ViewBag.Msg      = profile.error;
                        ViewBag.Country  = profile.Country;
                        ViewBag.Language = lang;
                        return(View("ConfirmChangePassword"));
                    }
                    if (!Codehelper.DefaultCountry.EqualsIgnoreCaseAndBlank(profile.Country))
                    {
                        return(RedirectToAction("SwitchSite", new { country = profile.Country, language = lang }));
                    }
                    return(RedirectToAction("Index", "Home", new { lang }));
                }
            }
            ModelState.AddModelError("", StringResource.INVALID_USERNAME_OR_PASSWORD);
            return(View(user));
        }