码迷,mamicode.com
首页 > Web开发 > 详细

MVC Form认证、权限、异常日志

时间:2014-11-19 17:58:32      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:http   io   ar   os   sp   java   for   on   log   

一、首先是登录:

public class AccountController : BaseController
{
public ActionResult Login()
{
//已经登录的,直接到默认首页
if (HttpContext.Request.IsAuthenticated)
{
return Redirect(FormsAuthentication.DefaultUrl);
}
return View();
}

[HttpPost]
public ActionResult Login(string userName, string userPassword, string isRemember)
{
if (userName == "admin" && userPassword == "111")
{
Person p = new Person() { Name = userName, Roles = "admin", Age = 23, Email = "xx@qq.com", Ip = MD5Helper.MD5Encrypt(Request.UserHostAddress) };
bool remenber = isRemember == null ? false : true;
//把用户对象保存在票据里
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddTicks(FormsAuthentication.Timeout.Ticks), remenber, p.ObjToJson());
//加密票据
string hashTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
if (remenber)
{
userCookie.Expires = DateTime.Now.AddTicks(FormsAuthentication.Timeout.Ticks);
}
Response.Cookies.Add(userCookie);

string returnUrl =HttpUtility.UrlDecode(Request["ReturnUrl"]);
if (string.IsNullOrEmpty(returnUrl))
{
return RedirectToAction("Index", "Home");
}
else
{
return Redirect(returnUrl);
}
}
else
{
ViewData["Tip"] = "用户名或密码有误!";
return View();
}
}
public ActionResult Logout()
{
//取消Session会话
Session.Abandon();
//删除Forms验证票证
FormsAuthentication.SignOut();
return RedirectToAction("Login", "Account");
}
}
}

二、权限验证 

public class AuthAttribute : ActionFilterAttribute
{
/// <summary>
/// 角色名称
/// </summary>
public string Code { get; set; }
/// <summary>
/// 验证权限(action执行前会先执行这里)
/// </summary>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//如果存在身份信息
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
Person person = new Models.UserInfo().UserLoginInfo;
string[] Role = person.Roles.Split(‘,‘);//获取所有角色

if (person.Ip != Common.MD5Helper.MD5Encrypt(HttpContext.Current.Request.UserHostAddress))
{
ContentResult Content = new ContentResult();
Content.Content = string.Format("<script type=‘text/javascript‘>alert(‘为了您的帐号安全,请重新登录!‘);window.location.href=‘{0}‘;</script>", FormsAuthentication.LoginUrl);
filterContext.Result = Content;
}
else if (!Role.Contains(Code))//验证权限
{
//验证不通过
ContentResult Content = new ContentResult();
Content.Content = "<script type=‘text/javascript‘>alert(‘您没有足够的权限!‘);history.go(-1);</script>";
filterContext.Result = Content;
}
}
else
{
ContentResult Content = new ContentResult();
Content.Content = string.Format("<script type=‘text/javascript‘>alert(‘请先登录!‘);window.location.href=‘{0}?ReturnUrl={1}‘;</script>", FormsAuthentication.LoginUrl, HttpUtility.UrlEncode(HttpContext.Current.Request.RawUrl));
filterContext.Result = Content;
}
}
}

三、/// 异常处理

public class BaseController : Controller
{
/// <summary>
/// 异常处理
/// </summary>
/// <param name="filterContext"></param>
protected override void OnException(ExceptionContext filterContext)
{
// 记录错误日志
ErrorLog.Write(filterContext);
}

}
}

MVC Form认证、权限、异常日志

标签:http   io   ar   os   sp   java   for   on   log   

原文地址:http://www.cnblogs.com/5tomorrow/p/4108370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!