public ActionResult Login(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { AuthDBDataContext db = new AuthDBDataContext(); List <ValidateUserLoginResult> lstStaff = db.ValidateUserLogin(model.LoginId, model.Password).ToList(); //アクセスログ用情報 short iSat = 0; int? iUserId = null; //string strIP = AppFunction.GetClientIpAddress(Request); string strPort = Request.Url.Port.ToString(); string strUserAgent = Request.ServerVariables.Get("HTTP_USER_AGENT"); string strSessionId = Session.SessionID; if (lstStaff.Count > 0) { //ログイン成功 iSat = 1; iUserId = lstStaff[0].iUserID; //try //{ // db.InsertAccessLog(iSat, iUserId, strIP, strPort, strUserAgent, DateTime.Now, strSessionId, "認証ok", iUserId.ToString()); //} //catch (Exception ex) //{ // AppLog.TraceLog(ex); //} //権限 string roles = string.Empty; if (lstStaff[0].iAccess == 1) { roles += "," + AppConst.RolesInSite.Level_Admin; } else { roles += "," + AppConst.RolesInSite.Level_Normal; } if (roles.Length > 0) { roles = roles.Substring(1); } FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, lstStaff[0].vEmail.ToString(), DateTime.Now, DateTime.Now.AddMinutes(120), true, roles); string encTicket = FormsAuthentication.Encrypt(authTicket); this.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); //FormsAuthentication.SetAuthCookie(lstStaff[0].vEmail,true); if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) { Session["NeedLoginAgent"] = true; return(Redirect(returnUrl)); } else { Session["NeedLoginAgent"] = true; //return RedirectToAction("Index", "Home"); return(RedirectToAction("index", "calldetail", new { area = "report" })); } } else { iSat = 2; //try //{ // db.InsertAccessLog(iSat, iUserId, strIP, strPort, strUserAgent, DateTime.Now, strSessionId, "認証NG", ""); //} //catch (Exception ex) //{ // AppLog.TraceLog(ex); //} ModelState.AddModelError("", "※ユーザー名またはパスワードが正しくありません。"); } } // ここで問題が発生した場合はフォームを再表示します return(View(model)); }
public ActionResult Login(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { AuthDBDataContext db = new AuthDBDataContext(); List<ValidateUserLoginResult> lstStaff = db.ValidateUserLogin(model.LoginId, model.Password).ToList(); //アクセスログ用情報 short iSat = 0; int? iUserId = null; //string strIP = AppFunction.GetClientIpAddress(Request); string strPort = Request.Url.Port.ToString(); string strUserAgent = Request.ServerVariables.Get("HTTP_USER_AGENT"); string strSessionId = Session.SessionID; if (lstStaff.Count > 0) { //ログイン成功 iSat = 1; iUserId = lstStaff[0].iUserID; //try //{ // db.InsertAccessLog(iSat, iUserId, strIP, strPort, strUserAgent, DateTime.Now, strSessionId, "認証ok", iUserId.ToString()); //} //catch (Exception ex) //{ // AppLog.TraceLog(ex); //} //権限 string roles = string.Empty; if (lstStaff[0].iAccess == 1) { roles += "," + AppConst.RolesInSite.Level_Admin; } else { roles += "," + AppConst.RolesInSite.Level_Normal; } if (roles.Length > 0) roles = roles.Substring(1); FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, lstStaff[0].vEmail.ToString(), DateTime.Now, DateTime.Now.AddMinutes(120), true, roles); string encTicket = FormsAuthentication.Encrypt(authTicket); this.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); //FormsAuthentication.SetAuthCookie(lstStaff[0].vEmail,true); if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) { Session["NeedLoginAgent"] = true; return Redirect(returnUrl); } else { Session["NeedLoginAgent"] = true; return RedirectToAction("Index", "Home"); } } else { iSat = 2; //try //{ // db.InsertAccessLog(iSat, iUserId, strIP, strPort, strUserAgent, DateTime.Now, strSessionId, "認証NG", ""); //} //catch (Exception ex) //{ // AppLog.TraceLog(ex); //} ModelState.AddModelError("", "※ユーザー名またはパスワードが正しくありません。"); } } // ここで問題が発生した場合はフォームを再表示します return View(model); }