YbRapidSolution.Mvc判断不同用户登录不同页面
AccountController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Web.Security;
using Newtonsoft.Json;
using Yb.Data.Provider;
using Yb.PermissionModel;
using Yb.PermissionModel.Provider;
using YbRapidSolution.Data;
using YbRapidSolution.Entities;
using YbRapidSolution.Mvc.Models;
using YbRapidSolution.Presenter.Compression;
using YbRapidSolution.Services; namespace YbRapidSolution.Mvc.Controllers.Security
{
[MvcCompression]
public class AccountController : Controller
{
private readonly ICustomerService _service;
public AccountController(ICustomerService service)
{
_service = service;
} #region 后台登录与注销
//
// GET: /Logon/
[AllowAnonymous]
public ActionResult Login()
{
return View(new LoginModel());
} [AllowAnonymous]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Login(LoginModel model)
{
if (!ModelState.IsValid)
{
if (string.IsNullOrWhiteSpace(model.UserName))
ViewBag.EasyUIMessage = "请输入用户名";
else if (string.IsNullOrWhiteSpace(model.Password))
ViewBag.EasyUIMessage = "请输入登录密码";
else
{
AuditLogApi.Info(string.Format("用户‘{0}’登录失败,IP地址:{1}", model.UserName, GetIP4Address()));
ViewBag.EasyUIMessage = "验证失败,必须输入用户名和密码";
}
return View(model);
}
try
{
var result = PersonApi.ValidatePerson(model.UserName, model.Password);
if (result!=null)
{
var psm = OrgApi.FindPersonMembersBy(result.ID).Where(c=>c.Status>).Select(c=>c.Parent);
var authCookie = FormsAuthentication.GetAuthCookie(model.UserName, model.RememberMe);
var ticket = FormsAuthentication.Decrypt(authCookie.Value);
var userModel = new IdentifyModel
{
ID = result.ID,
UserName = result.UserName,
DisplayName = result.DisplayName,
Code = result.Code,
Email = result.Email,
IDCard = result.IDCard,
PasswordTimeLimit = result.PasswordTimeLimit,
MainOrgID = result.MainOrgID,
Lang = result.Lang,
Theme = result.Theme
};
userModel.SetOrgIds(psm); var userData = JsonConvert.SerializeObject(userModel); //var userData = "1";
//var userData = SerializerUtility.StringSerialize(userModel);
var newTicket = new FormsAuthenticationTicket(
ticket.Version, ticket.Name, ticket.IssueDate,
ticket.Expiration, ticket.IsPersistent, userData);
// 将新的Ticke转变为Cookie值,然后添加到Cookies集合中
authCookie.Value = FormsAuthentication.Encrypt(newTicket);
this.HttpContext.Response.Cookies.Add(authCookie); AuditLogApi.Info(string.Format("用户‘{0}’登录成功,IP地址:{1}", model.UserName, GetIP4Address())); // 获得 来到登录页之前的页面,即url中return参数的值
string url = FormsAuthentication.GetRedirectUrl(model.UserName, model.RememberMe);
return Redirect(url);
}
ViewBag.EasyUIMessage = "错误的用户名或密码";
return View(model);
}
catch (Exception er)
{
ViewBag.EasyUIMessage = er.Message;
return View(model);
}
} [AllowAnonymous]
public ActionResult SignOut()
{
FormsAuthentication.SignOut();
return Redirect("~/Account/Login");
} #endregion #region 前台登录、注销方法 [AllowAnonymous]
public ActionResult Logon()
{
var model = new LoginModel();
var cookies = this.Request.Cookies;
var cookie=cookies.Get("Customer");
if (cookie!=null && cookie.HasKeys)
{
model.UserName= cookie["tel"];
}
return View(model);
} [AllowAnonymous]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Logon(LoginModel model)
{
var tel = "";
var pwd = "";
if (!ModelState.IsValid)
{
if (string.IsNullOrWhiteSpace(model.UserName))
{
ViewBag.EasyUIMessage = "请输入电话号码";
return View("Logon", model);
}
if (string.IsNullOrWhiteSpace(model.Password))
{
ViewBag.EasyUIMessage = "请输入验证码";
return View("Logon", model);
}
}
tel = model.UserName.Trim(); if (!IsMobilePhone(tel))
{
ViewBag.EasyUIMessage = "手机号码格式不正确";
return View("Logon", model);
}
pwd = model.Password.Trim();
if (!IsValidCode(pwd))
{
ViewBag.EasyUIMessage = "验证码格式不对,验证码为4为数字";
return View("Logon", model);
} try
{
var user = _service.GetByTel(model.UserName);
if (user != null)
{
if (user.DeleteStatus == (int)DeleteStatus.Deleted)
{
ViewBag.EasyUIMessage = "当前用户已被禁用,请联系管理员";
return View("Logon", model);
} if (user.SendDate < DateTime.Now.AddMinutes(-))
{
ViewBag.EasyUIMessage = "验证码已过期,请重新获取验证码.";
return View("Logon", model);
} if (user.ValidationCode!=model.Password)
{
ViewBag.EasyUIMessage = "验证码错误,请确认后重新输入.";
return View("Logon", model);
} var authCookie = FormsAuthentication.GetAuthCookie(model.UserName, true);
var ticket = FormsAuthentication.Decrypt(authCookie.Value);
var userModel = new IdentifyModel
{
ID = user.Id,
UserName = model.UserName
}; var userData = JsonConvert.SerializeObject(userModel);
//var userData = "1";
//var userData = SerializerUtility.StringSerialize(userModel);
var newTicket = new FormsAuthenticationTicket(
ticket.Version, ticket.Name, ticket.IssueDate,
ticket.Expiration.AddYears(), true, userData);
// 将新的Ticke转变为Cookie值,然后添加到Cookies集合中
authCookie.Value = FormsAuthentication.Encrypt(newTicket);
authCookie.Expires = ticket.Expiration.AddYears();
this.HttpContext.Response.Cookies.Add(authCookie); var cookie = new HttpCookie("Customer");
cookie["tel"] = model.UserName;
cookie.Expires = DateTime.Now.AddYears();
this.Response.Cookies.Add(cookie); AuditLogApi.Info(string.Format("用户‘{0}’登录成功,IP地址:{1}", model.UserName, GetIP4Address())); var url = "";
if (this.TempData.ContainsKey("Url"))
{
var value = this.TempData["Url"];
if (value != null)
{
url = value.ToString();
}
}
else
{
url = FormsAuthentication.GetRedirectUrl(model.UserName, model.RememberMe);
}
if (string.IsNullOrWhiteSpace(url) || url.ToLower().Contains("/admin"))
{
return RedirectToAction("Index", "Home");
}
return Redirect(url);
}
ViewBag.EasyUIMessage = "请首先获取验证码";
return View("Logon", model);
}
catch (Exception er)
{
ViewBag.EasyUIMessage = er.Message;
return View("Logon", model);
}
}
[AllowAnonymous]
public ActionResult SignOutHome()
{
return Redirect("/Account/Logon");
} #endregion #region Help 方法 public static bool IsMobilePhone(string input)
{
Regex regex = new Regex("^1\\d{10}$");
return regex.IsMatch(input);
}
public static bool IsValidCode(string input)
{
Regex regex = new Regex("\\d{4}");
return regex.IsMatch(input);
}
public static string GetIP4Address()
{
string IP4Address = String.Empty; foreach (IPAddress IPA in Dns.GetHostAddresses(System.Web.HttpContext.Current.Request.UserHostAddress))
{
if (IPA.AddressFamily.ToString() == "InterNetwork")
{
IP4Address = IPA.ToString();
break;
}
} if (IP4Address != String.Empty)
{
return IP4Address;
} foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (IPA.AddressFamily.ToString() == "InterNetwork")
{
IP4Address = IPA.ToString();
break;
}
}
return IP4Address;
} #endregion
}
}
YbRapidSolution.Mvc判断不同用户登录不同页面的更多相关文章
- /*用户登录注册页面输入框的设置*/<span>的使用
<!DOCTYPE html> /*用户登录注册页面输入框的设置*/ <html lang="en"> <head> <meta char ...
- MVC过滤器实现用户登录验证
前言当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null),我们知道在WebForm中可以定义一个BasePage类让他继承System.Web.UI.Page,重写它的 ...
- ASP.NET MVC实现单用户登录
现在许多网站都要求登录后才能进行进一步的操作,当不允许多用户同时登录一个帐号时,就需要一种机制,当再登录一个相同的帐号时,前面登录的人被挤下线,或者禁止后面的人登录.这里实现的是前一种功能. 网上有许 ...
- servlet技术--使用注解模拟用户登录实现页面跳转
文章目录 1.servlet体系结构 2.servlet技术特点 3.servlet和jsp的区别 4.servlet开发 1.servlet体系结构 servlet实质就是按servlet规范编写的 ...
- MVC判断用是否登录了平台
需求就是要求有些页面需要用户登陆了之后才能访问,那么就需要是否登录验证,直接上代码: 这个可以单独写到一个类里面: WebAuthenUsers.cs: using System; using Sys ...
- ASP.NET MVC项目演练:用户登录
ASP.NET MVC 基础入门 http://www.cnblogs.com/liunlls/p/aspnetmvc_gettingstarted.html 设置默认启动页面 public clas ...
- 通过配置http拦截器,来进行ajax请求验证用户登录的页面跳转
在.NET中验证用户是否登录或者是否过期,若需要登录时则将请求转向至登录页面. 这个流程在进行页面请求时是没问题的,能正确进行页面跳转. 然而在使用xmlhttprequest时,或者jq的getJs ...
- ASP.Net MVC Filter验证用户登录
一.Filter是什么 ASP.NetMVC模式自带的过滤器Filter,是一种声明式编程方式,支持四种过滤器类型,各自是:Authorization(授权),Action(行为),Result(结果 ...
- JDBC MVC框架实现用户登录
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写 1.实体entity package com.uplooking. ...
随机推荐
- 关于<a href='javascript:function()'>
<a href='javascript:function()'> 这样写是为了让这个链接不要链接到新页面转而执行一段js代码.和onclick能起到同样的效果,一般来说,如果要调用脚本还是 ...
- 【转】用树莓派搭建web服务器
本文将详细介绍如何在树莓派上配置服务器,和<教你在Xubuntu上搭建LAMP服务器>有些类似,多了一些介绍在树莓派上的不同步骤的地方. 这种服务器的配置被称为LAMP,是最流行的服务器配 ...
- 在express项目中有效组织和使用mongoose
平凡之路 1.创建express项目 express mongooseExpress 2.最简express var express = require("express"); v ...
- Spark Programming--Transformations
map 将RDD中的每个数据项,一对一的映射关系,RDD数目不变,分区数也不变 例子: 数据集: map操作: flatMap 和map一样,但是会拆分每一个map之后的list,可以理解为一对多(注 ...
- JS的基础类型与引用类型
两种类型: ECMAScript变量包含两种不同类型的值:基本类型值.引用类型值: 基本类型值:指的是保存在栈内存中的简单数据段: 引用类型值:指的是那些保存在堆内存中的对象,意思是,变量中保存的实际 ...
- Selenium2学习-018-WebUI自动化实战实例-016-自动化脚本编写过程中的登录验证码问题
日常的 Web 网站开发的过程中,为提升登录安全或防止用户通过脚本进行黄牛操作(宇宙最贵铁皮天朝魔都的机动车牌照竞拍中),很多网站在登录的时候,添加了验证码验证,而且验证码的实现越来越复杂,对其进行脚 ...
- 前端面试题(html篇)
前端面试题(html篇)
- Hadoop学习笔记: HDFS
注:该文内容部分来源于ChinaHadoop.cn上的hadoop视频教程. 一. HDFS概述 HDFS即Hadoop Distributed File System, 源于Google发表于200 ...
- instruments 教程
https://www.raywenderlich.com/97886/instruments-tutorial-with-swift-getting-started
- SpringJUnit4ClassRunner拉起来的单元测试怎么装配Container实例
由于历史代码的原因,产品中部分spring装配的实例需要通过Container的实现类(自定义的)去获取.那么当在单元测试中怎么实例化这个Container实现呢? 实例化Container实现需要A ...