在Asp.net中实现登录验证可以设置用户登录验证页面为默认的路由处理路径,访问其它页面时候,在页面控制器中增加 CheckAuthority属性,要求进行登录认证后才能访问相关控制器,CheckAuthority属性定义代码如下示例: - //==============属性继承自AuthorizeAttribute,需重写OnAuthorization方法===========
- public class CheckAuthority: AuthorizeAttribute
- {
- public override void OnAuthorization(AuthorizationContext filterContext)
- {
- if (HttpContext.Current.Session["UserID"] == null || !HttpContext.Current.Request.IsAuthenticated)
- {
- if (filterContext.HttpContext.Request.IsAjaxRequest())
- {
- filterContext.HttpContext.Response.StatusCode = 302; //Found Redirection to another page. Here- login page. Check Layout ajaxError() script.
- filterContext.HttpContext.Response.End();
- }
- else
- {
- filterContext.Result = new RedirectResult(System.Web.Security.FormsAuthentication.LoginUrl + "?ReturnUrl=" +
- filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.Raw
复制代码 来源:https://blog.csdn.net/u012846041/article/details/82383510 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |