http://www.jb51.net/article/20147.htm 引用
<authentication mode="Forms"><!--权限受到阻碍将会跳转到这个页面,不写找不到的时候则报404-->
<forms loginUrl="~/ExcelEport/Login" timeout="2880" />
</authentication>
// [Authorize(Roles = "Admin")]//只有通过用户才可以访问该方法
public ActionResult likJinbulai()//你要设置权限的页面
{
//判断通过身份验证的用户是否有权限访问本页面
FormsIdentity id = (FormsIdentity)HttpContext.User.Identity;
//判断通过身份验证的用户是否是Admin角色
if (id.Ticket.UserData.Contains("Admin"))
{
return View();
//跳转到访问权限不够的错误提示页面
}
else
{
ViewBag.Authorize = false;
return View("youjinbulai");
//从哪里来回哪里去,在湖区的页面添加以下代码
//@if (@ViewBag.Authorize!=null)
//{
// if (!(@ViewBag.Authorize) )
// {
// <script type="text/javascript">
// $(function () {
// alert("您没有权限");
// });
// </script>
// }
//}
}
}
///登陆用户处理Action
public ActionResult LoginHandler(string userName, string userPwd,string role)
{
//mvc
////该用户通过验证后,会得到数据库字段Roles
////验证成功直接写入客户端cookie票据
//FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
//1,
//userName,//用户登陆进来的用户名
//DateTime.Now,
//DateTime.Now.AddMinutes(20),
//true,//是否存在持久,存储在客户端
//role//登陆用户的角色写入登陆用户的角色
//);
////加密身份验证票据
//string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
////把准备好的cookie加入到响应流中
//System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
//authCookie.Expires = authTicket.Expiration;//票据过期时间
////把准备好的cookie加入到响应流中
//System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
//web
//Forms身份验证初始化
FormsAuthentication.Initialize();
//验证用户输入并得到登录用户,txtName是用户名称,txtPassword是登录密码
//UserModel um = ValidUser(txtName.Text.Trim(), txtPassword.Text.Trim());
//创建身份验证票据
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
userName,
DateTime.Now,
DateTime.Now.AddMinutes(30),
true,
role,//用户所属的角色字符串
FormsAuthentication.FormsCookiePath);
//加密身份验证票据
string hash = FormsAuthentication.Encrypt(ticket);
//创建要发送到客户端的cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
if (ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration;
}
//把准备好的cookie加入到响应流中
Response.Cookies.Add(cookie);
//转发到请求的页面
//Response.Redirect(FormsAuthentication.GetRedirectUrl(userName, false));
return View("youjinbulai");
}
//注销票据
public ActionResult ClearTicket()
{
//注销票据
FormsAuthentication.SignOut();
string script = "alert('您已经安全退出了!');";
return JavaScript(script);
}
//以下写在Global.asax
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//mvc
//HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
//if (authCookie == null || authCookie.Value == "")
//{
// return;
//}
//FormsAuthenticationTicket authTicket = null;
//try
//{
// authTicket = FormsAuthentication.Decrypt(authCookie.Value);
//}
//catch
//{
// return;
//}
//string[] roles = authTicket.UserData.Split(new char[] { ';' });
//if (Context.User != null)
//{
// Context.User = new System.Security.Principal.GenericPrincipal(Context.User.Identity, roles);
//}
//webform
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
string userData = ticket.UserData;
string[] roles = userData.Split(',');
//重建HttpContext.Current.User,加入用户拥有的角色数组
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
}
}
}
}