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. ...
随机推荐
- 流媒体学习四------- ortp队列的实现
在ortp中实现了一个通用的队列,每一个队列包括三个实体,分别是队列.消息块和数据块,这三个实体分别对应queue_t.msgb和datab结构体. queue_t的定义如下所示: typedef s ...
- java编程技巧
欢迎提出建议指出错误互相交流. 1.统计对象数量,比如统计一共发射了多少颗子弹. public class Bullet { public static int count = 0; public B ...
- 视频播放器开发中遇到的一些小问题MPMoviePlayerController
1 开发环境是 xcode6 ipad3真机 ios8.1.1越狱 需要添加以下代码 ,否则真机测试没有外音,只有耳机 NSError *setCategoryError = nil; ...
- asp.net批量删除XML节点失败的原因及解决办法
今天操作XML的时候,用到了批量循环删除节点.出现了问题,即循环未结束,程序就跳出循环.搞了好久才弄明白. 解决前的代码: XmlNodeList items = xn.ChildNodes; //获 ...
- 关于actionscript中新建一个sprite,设置大小(宽高)的问题。
有一定as3开发经验的童鞋应该知道,新建一个sprite,是无法设置大小的,即时设置了,也不会生效,宽高还是为0,据说反而有副作用(http://www.cnblogs.com/yjmyzz/arch ...
- 图像分割之(二)Graph Cut(图割)
zouxy09@qq.com http://blog.csdn.net/zouxy09 上一文对主要的分割方法做了一个概述.那下面我们对其中几个比较感兴趣的算法做个学习.下面主要是Graph Cut, ...
- 在Visual Studio 2015中运行OPENGL
Starting an OpenGL project in VS 2015 is really easy, thanks to the NupenGL.Core nuget package. Here ...
- Android 对Map按key和value分别排序
一.理论准备 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红黑树(Red-Black tre ...
- mysql运行参数详解
1, 查看MySQL服务器配置信息 mysql> show variables; 2, 查看MySQL服务器运行的各种状态值 mysql> show global status; 3, 慢 ...
- 更改AlertView背景
UIAlertView *theAlert = [[[UIAlertViewalloc] initWithTitle:@"Atention" message: @"I'm ...