当前位置: 首页 > news >正文

[mvc] 简单的forms认证

1、在web.config的system.web节点增加authentication节点,定义如下:

  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880">
        <credentials passwordFormat="Clear">
          <user name="user" password="pwd001"/>
          <user name="admin" password="pwd002"/>
        </credentials>
      </forms>
    </authentication>
  </system.web>

2,新增AccountController。

    public class AccountController : Controller
    {
        // 用于初期表示用
        public ActionResult Login()
        {
            return View();
        }

        // 登录按钮
        [HttpPost]
        public ActionResult Login(string username, string password, string returnUrl)
        {
            bool result = FormsAuthentication.Authenticate(username, password);
            if (result)
            {
                FormsAuthentication.SetAuthCookie(username, false);
                return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
            }
            else
            {
                ModelState.AddModelError("", "Incorrect username or password");
                return View();
            }
        }
    }

3、Login.cshtml

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
</head>
<body>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary()
        <p><label>Username:</label><input name="username" type="text" /></p>
        <p><label>Password:</label><input name="password" type="password" /></p>
        <input type="submit" value="Log in"/>
    }
</body>
</html>

4、浏览器输入http://localhost:44324/Account/Login,输入web.config中定义的用户名和密码,成功就会进入Admin/Index页面。

5、其他页面如何进行认证?

1)在action中加Request.IsAuthenticated判断

    public class AdminController : Controller
    {
        // GET: Admin
        public string Index()
        {
            if (!Request.IsAuthenticated)
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            return "welcome to Admin page!";
        }
    }

2)在action方法上加Authorize特性

    public class AdminController : Controller
    {
        // GET: Admin
        [Authorize]
        public string Index()
        {
            return "welcome to Admin page!";
        }
    }

3)在controller上加Authorize特性(所有的action都会应用上)

    [Authorize]
    public class AdminController : Controller
    {
        // GET: Admin
        public string Index()
        {
            return "welcome to Admin page!";
        }
    }

 

相关文章:

  • nmap 端口扫描工具
  • NGINX发布支持动态配置的开源Web服务器
  • Java List集合
  • C++11 lambda表达式与函数对象
  • 人人都能学会的python编程教程8:条件判断与循环
  • #考研#计算机文化知识1(局域网及网络互联)
  • homebridge安装问题解决
  • DesignPattern(三)结构型模式(上)
  • 八周一次课 10.23 linux任务计划cron 10.24 chkconfig工具 10.25 systemd管理服务 10.26 unit介绍 10.27 target介绍...
  • 最高优先级算法——进程调度
  • 前端小知识
  • 测试的方法!
  • springboot 项目mybatis plus 设置 jdbcTypeForNull (oracle数据库需配置JdbcType.NULL, 默认是Other)...
  • redis学习之redis的安装(linux)
  • Maven项目如何将自定义文件添加到META-INF目录下
  • Apache的基本使用
  • canvas实际项目操作,包含:线条,圆形,扇形,图片绘制,图片圆角遮罩,矩形,弧形文字...
  • CentOS从零开始部署Nodejs项目
  • css属性的继承、初识值、计算值、当前值、应用值
  • C学习-枚举(九)
  • DataBase in Android
  • express.js的介绍及使用
  • Java知识点总结(JavaIO-打印流)
  • MySQL-事务管理(基础)
  • quasar-framework cnodejs社区
  • Redis字符串类型内部编码剖析
  • TypeScript实现数据结构(一)栈,队列,链表
  • win10下安装mysql5.7
  • 基于 Babel 的 npm 包最小化设置
  • 记一次和乔布斯合作最难忘的经历
  • 扑朔迷离的属性和特性【彻底弄清】
  • 前嗅ForeSpider中数据浏览界面介绍
  • 以太坊客户端Geth命令参数详解
  • 云大使推广中的常见热门问题
  • 字符串匹配基础上
  • ionic入门之数据绑定显示-1
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • 数据库巡检项
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • #QT(智能家居界面-界面切换)
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (BFS)hdoj2377-Bus Pass
  • (二) Windows 下 Sublime Text 3 安装离线插件 Anaconda
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (一)为什么要选择C++
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • (转)使用VMware vSphere标准交换机设置网络连接
  • ****** 二 ******、软设笔记【数据结构】-KMP算法、树、二叉树
  • .Net 4.0并行库实用性演练
  • .NET delegate 委托 、 Event 事件
  • .Net MVC4 上传大文件,并保存表单
  • .NET Standard、.NET Framework 、.NET Core三者的关系与区别?