web.config配置

<authentication mode="Forms">
<forms loginUrl="~/Login/Index" timeout="30" slidingExpiration="true"></forms>
</authentication>
  • 1
  • 2
  • 3

增加一个Attribute类,继承自AuthorizeAttribute

public class CustomAuthorzieAttribute : AuthorizeAttribute
{
private string _controllerName = string.Empty;
private string _actionName = string.Empty; /// <summary>
/// base.OnAuthorization(filterContext)中会调用AuthorizeCore函数
///
/// 当AuthorizeCore返回false,则会继续调用HandleUnauthorizedRequest进行处理
///
/// 所以OnAuthorization是该类的总入口
///
/// </summary>
/// <param name="filterContext"></param>
public override void OnAuthorization(AuthorizationContext filterContext)
{
_controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
_actionName = filterContext.ActionDescriptor.ActionName; base.OnAuthorization(filterContext);
} /// <summary>
/// base.OnAuthorization来调用
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (_controllerName.ToLower() == "login")
{//登陆界面,不用身份认证,直接返回true
return true;
} if (!httpContext.User.Identity.IsAuthenticated)
{
return false;
} return true;
} /// <summary>
/// 当AuthorizeCore返回false时候调用
/// </summary>
/// <param name="filterContext"></param>
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
base.HandleUnauthorizedRequest(filterContext);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

主要是去除登陆页面的验证判断

在App_Start文件夹中的FilterConfig中增加

public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new PermManagerWeb.Controllers.CustomAuthorzieAttribute());
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

登陆页面处理:

[HttpPost]
public ActionResult DoLogin(LoginInfoViewModel loginInfo)
{
if (ModelState.IsValid)
{//输入验证成功
bool bLoginOK = Login.LoginSys(GetDataAccess(), loginInfo.UserName, loginInfo.UserPassword);
if (bLoginOK)
{//登陆成功
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
loginInfo.UserName,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
Request.UserHostAddress,
FormsAuthentication.FormsCookiePath);
string strCookie = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, strCookie);
Response.Cookies.Add(authCookie); return RedirectToAction("../Main");
}
else
{//登陆失败
return View("Index");
}
}
else
{//输入验证失败
return View("Index");
}
}

asp.net mvc forms身份认证的更多相关文章

  1. (转)Asp.Net MVC中身份认证和授权

    MVC自带的ActionFilter 在Asp.Net WebForm的中要做到身份认证微软为我们提供了三种方式,其中最常用的就是我们的Form认证,需要配置相应的信息.例如下面的配置信息: < ...

  2. forms身份认证仍然能访问html页面解决办法

    asp.net的forms身份认证保护是一个非常棒的东西,用VS2010创建一个Web应用程序即可看到范例 在web.config中配置 <authentication mode="F ...

  3. 细说ASP.NET Forms身份认证

    阅读目录 开始 ASP.NET身份认证基础 ASP.NET身份认证过程 如何实现登录与注销 保护受限制的页面 登录页不能正常显示的问题 认识Forms身份认证 理解Forms身份认证 实现自定义的身份 ...

  4. 简单的ASP.NET Forms身份认证

    读了几篇牛人的此方面的文章,自己也动手做了一下,就想有必要总结一下.当然我的文章质量自然不能与人家相比,只是写给从没有接触过这个知识点的朋友. 网站的身份认证我以前只知道session,偶然发现一些牛 ...

  5. IE11下ASP.NET Forms身份认证无法保存Cookie的问题

    IE11下ASP.NET Forms身份认证无法保存Cookie的问题 折腾了三四天,今天才找到资料,解决了. 以下会转贴,还没来得及深究,先放着,有空再学习下. ASP.NET中使用Forms身份认 ...

  6. 关于Asp.Net Forms身份认证

    Asp.Net管道式的构建个我们提供了通过IHttpMoudle来订阅管线事件来达到干预HTTP请求的目的,Asp.Net的身份认证正是通过此种方式来对请求来执行身份认证的,这篇文章仅仅谈论Forms ...

  7. 【转】权限管理学习 一、ASP.NET Forms身份认证

    [转]权限管理学习 一.ASP.NET Forms身份认证 说明:本文示例使用的VS2017和MVC5. 系统无论大小.牛逼或屌丝,一般都离不开注册.登录.那么接下来我们就来分析下用户身份认证. 简单 ...

  8. ASP.NET Forms身份认证详解

    ASP.NET身份认证基础 在开始今天的内容之前,我想有二个最基础的问题首先要明确: 1. 如何判断当前请求是一个已登录用户发起的? 2. 如何获取当前登录用户的登录名? 在标准的ASP.NET身份认 ...

  9. 细说ASP.NET Forms身份认证 别人写的不过很透彻就转来了以后用时再看

    阅读目录 开始 ASP.NET身份认证基础 ASP.NET身份认证过程 如何实现登录与注销 保护受限制的页面 登录页不能正常显示的问题 认识Forms身份认证 理解Forms身份认证 实现自定义的身份 ...

随机推荐

  1. 在Unity3D中连接WCF服务端

    服务端不多讲解,有一处需要改的地方.具体服务端请看WCF入门学习2-控制台做为宿主 建议实际项目不要拿去用,毕竟是mono不是原生.net.或许是个坑 由于Unity的mono版本问题不能直接用net ...

  2. DrawPrimitivesTest

    #ifndef _DRAW_PRIMITIVES_TEST_H_ #define _DRAW_PRIMITIVES_TEST_H_ ////----#include "cocos2d.h&q ...

  3. 每日英语:Apple's Latest iPhone Puts Focus Back on Fingerprint Security

    Apple's latest product launch could breathe new life into a technology that failed to take hold the ...

  4. In search of the perfect URL validation regex

    To clarify, I’m looking for a decent regular expression to validate URLs that were entered as user i ...

  5. feginclient和hystrix的配置

    1.如果设置了 feign: hystrix: enabled: true 则 @FeignClient(value = "service-hi",configuration = ...

  6. 【转载】用Pwnage + Redsnow 制作完美越狱固件

    [转载] 现在貌似IOS 7.X系 大行其道,就算不是IOS7.X ,很多人也装着IOS 6.X系. 进入正文前首先介绍一下自己的装备 设备:iphone4 GSM 大陆行货 (内部版本3,1) 为什 ...

  7. Android开发日记(二)

    HashMap<String, Object> map;定义一个HashMap用来传递字符 TextView textView_JobTitle=(TextView)findViewByI ...

  8. rabbitmq系列(一)-基本概念理解

    1.简介 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue protocol)的开源实现.AMQP高级消息队列,说白了就是一个开源的消息中间件.它能解决 ...

  9. 【Unity】UGUI的Text各种小问题

    Text:用中文输入法时,无法输入汉字.输入了拼音后,按回车键无反应.目前的办法是在别的地方打好字后复制过来. Font:字体必须选一个,选None则文字变成一串黑色方块. Font Size:文字大 ...

  10. 线程安全,有状态,无状态的对象<转>

    进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位.线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.另外 ...