首页 技术 正文
技术 2022年11月16日
0 收藏 348 点赞 2,827 浏览 2623 个字

前沿:

一般情况下,在我们做访问权限管理的时候,会把用户的正确登录后的基本信息保存在Session中,以后用户每次请求页面或接口数据的时候,拿到

Session中存储的用户基本信息,查看比较他有没有登录和能否访问当前页面。

Session的原理,也就是在服务器端生成一个SessionID对应了存储的用户数据,而SessionID存储在Cookie中,客户端以后每次请求都会带上这个

Cookie,服务器端根据Cookie中的SessionID找到存储在服务器端的对应当前用户的数据。

FormsAuthentication是微软提供给我们开发人员使用,做身份认证使用的。通过该认证,我们可以把用户Name 和部分用户数据存储在Cookie中,

通过基本的条件设置可以,很简单的实现基本的身份角色认证。

1.配置项

在网站根目录配置web.config

<authentication>
<forms name=".ASPXAUTH" loginUrl="account/index" defaultUrl="http://www.baidu.com" protection="All" timeout="" path="/" requireSSL="false" slidingExpiration="true" enableCrossAppRedirects="false" cookieless="UseDeviceProfile" domain="" ></forms>
</authentication>

2.控制器代码

 public class accountController : Controller
{
// GET: account
public ActionResult Index()
{
return View();
}
[Authentication]
public ActionResult Demo() => View();
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(string username,string userpwd)
{
List<LoginVm> userlist = new List<LoginVm>()//模拟数据
{
new LoginVm() { name="Zara", pwd="",State=},
new LoginVm(){name="aaaa",pwd="",State=}
}; if (!ModelState.IsValid)
{
return View();
}
bool status = Request.IsAuthenticated;
LoginVm vm = userlist.FirstOrDefault(u => u.name == username && u.pwd == userpwd);
JavaScriptSerializer serial = new JavaScriptSerializer();
//判断是否存在 且状态ok
if (vm!=null)
{
if (vm.State==)Content("您倍封号"); FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
, vm.name, DateTime.Now, DateTime.Now.AddMinutes(), false, serial.Serialize(vm));
string encrytedTicket = FormsAuthentication.Encrypt(authTicket);//创建票据
//响应客户端
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,encrytedTicket);
HttpContext.Response.Cookies.Add(authCookie);
}
return View();
}
}

3.过滤器方面

 /// <summary>
/// 该过滤器为网站提供服务
/// 服务内容:行为添加标记即可进行过滤.不用在每个action中进行过滤!
/// </summary>
public class AuthenticationAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{ if (!filterContext.RequestContext.HttpContext.Request.IsAuthenticated)
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Result = new JsonResult
{
Data = new
{
Status = -,
Message = "登录过期,请重新登录!"
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
else
{
FormsAuthentication.RedirectToLoginPage();//重定向会登录页
}
}
else
{
var cookie = filterContext.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
//解密用户票据
var ticket = FormsAuthentication.Decrypt(cookie.Value);
//将密文映射到实体模型
LoginVm admin = new JavaScriptSerializer().Deserialize<LoginVm>(ticket.UserData);
//将数据放到ViewData里 方面页面使用
filterContext.Controller.ViewData["username"] = admin.name;
filterContext.Controller.ViewData["userpwd"] = admin.pwd;
}
//Don't forget this one
base.OnActionExecuting(filterContext);
}
}

我们可以在aciton行为中添加需要登录的视图,这样封装一起就不用一个一个在控制器搞了…

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,033
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,862