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判断不同用户登录不同页面的更多相关文章

  1. /*用户登录注册页面输入框的设置*/<span>的使用

    <!DOCTYPE html> /*用户登录注册页面输入框的设置*/ <html lang="en"> <head> <meta char ...

  2. MVC过滤器实现用户登录验证

    前言当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null),我们知道在WebForm中可以定义一个BasePage类让他继承System.Web.UI.Page,重写它的 ...

  3. ASP.NET MVC实现单用户登录

    现在许多网站都要求登录后才能进行进一步的操作,当不允许多用户同时登录一个帐号时,就需要一种机制,当再登录一个相同的帐号时,前面登录的人被挤下线,或者禁止后面的人登录.这里实现的是前一种功能. 网上有许 ...

  4. servlet技术--使用注解模拟用户登录实现页面跳转

    文章目录 1.servlet体系结构 2.servlet技术特点 3.servlet和jsp的区别 4.servlet开发 1.servlet体系结构 servlet实质就是按servlet规范编写的 ...

  5. MVC判断用是否登录了平台

    需求就是要求有些页面需要用户登陆了之后才能访问,那么就需要是否登录验证,直接上代码: 这个可以单独写到一个类里面: WebAuthenUsers.cs: using System; using Sys ...

  6. ASP.NET MVC项目演练:用户登录

    ASP.NET MVC 基础入门 http://www.cnblogs.com/liunlls/p/aspnetmvc_gettingstarted.html 设置默认启动页面 public clas ...

  7. 通过配置http拦截器,来进行ajax请求验证用户登录的页面跳转

    在.NET中验证用户是否登录或者是否过期,若需要登录时则将请求转向至登录页面. 这个流程在进行页面请求时是没问题的,能正确进行页面跳转. 然而在使用xmlhttprequest时,或者jq的getJs ...

  8. ASP.Net MVC Filter验证用户登录

    一.Filter是什么 ASP.NetMVC模式自带的过滤器Filter,是一种声明式编程方式,支持四种过滤器类型,各自是:Authorization(授权),Action(行为),Result(结果 ...

  9. JDBC MVC框架实现用户登录

    MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写 1.实体entity package com.uplooking. ...

随机推荐

  1. Dlib is a modern C++ toolkit(非常全面的类库)

    http://dlib.net/ http://download.csdn.net/detail/lajuedan2508/9726225 http://download.csdn.net/detai ...

  2. Python 汉字简体和繁体的相互转换

    其实利用python实现汉字的简体和繁体相互转早有人做过,并发布到github上了,地址:https://github.com/skydark/nstools/tree/master/zhtools ...

  3. input[type=checkbox]

    一个问题,今天用jquery-1.11.3.min.js时遇到的关于input复选框的问题. 类似于以下代码: <ul class="demo">  <li> ...

  4. How To Set Up Apache with a Free Signed SSL Certificate on a VPS

    Prerequisites Before we get started, here are the web tools you need for this tutorial: Google Chrom ...

  5. Selenium2学习-009-WebUI自动化实战实例-007-Selenium 8种元素定位实战实例源代码(百度首页搜索录入框及登录链接)

    此 文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,通过 ID.name.xpath.cssSelector.linkText.className.partialLinkTe ...

  6. 如何将XML文件写入数据库

    将xml文件转成string public string XMLDocumentToString(XmlDocument doc) { MemoryStream stream = new Memory ...

  7. MFC之目录结构及消息流转(一)

    跟上时代,用vs2010, 新建一个MFC应用程序Helloworld. 目录结构: 所有文件分为6个部分:解决方案相关文件.工程相关文件.应用程序头文件和源文件.资源文件.预编译头文件和编译链接生成 ...

  8. NSOperationQueue 多线程

    staticNSOperationQueue * queue; - (void)viewDidLoad { [superviewDidLoad]; queue = [[NSOperationQueue ...

  9. MySQL性能优化(一)

    MySQL参数:innodb_flush_log_at_trx_commit和sync_binlog innodb_flush_log_at_trx_commit和sync_binlog是MySQL的 ...

  10. dd命令使用详解

    dd命令使用详解 http://www.cnblogs.com/qq78292959/archive/2012/02/23/2364760.html 1.命令简介 dd 的主要选项: 指定数字的地方若 ...