1:前台编写一个登录界面,这里为了简化,只有用户名和密码

代码如下:

<form method="post" action="/User/CheckLogin">
<div>
<span>用户名</span><input type="text" id="username" name="username"/><br/>
<span>密码</span><input type="text" id="password" name="password"/><br />
<input type="submit" id="submit" value="登录" />
</div>
</form>

2:后台CheckLogin方法中,验证用户名和密码,

  需要using System.Web.Security;

            if (new UserService().ValidateUser(username, password))//验证方法
{
FormsAuthentication.RedirectFromLoginPage(username, true);//个人理解就像SESSION中写入这个用户,后面可以读到
return Content("aaa");
}

3:配置WEB.CONFIG文件

<system.web>//这个节点下
<authentication mode="Forms">
<forms
name=".checklogin"
loginUrl="/User/UserLogin"
defaultUrl="/index.htm"
protection="All"
timeout="30"
path="/"
requireSSL="false"
slidingExpiration="false"
enableCrossAppRedirects="false"
cookieless="UseDeviceProfile"
domain=""
/>
</authentication>

4:使用

只需要在需要用的地方(一般是方法,或者类)加上[Authorize]

如:

   [Authorize]
public class InitDataToolsController : Controller
{
//.....
}

再如:

 [HttpPost]
[OpenSessionInViewFilter]
[Authorize]
public ActionResult addPlainLocation(string locationCode, string locationType, string comments)
      {}

如果验证不通过会自动调到登录界面,这个是在WEB.CONFIG文件中配置的

C#中登录验证FormsAuthentication的更多相关文章

  1. jsp 中登录验证 注销 的模版

    用户名密码验证模版     <%@page import="com.jerehedu.bao.User"%> <%@ page language="ja ...

  2. TP中登录验证

    loginpro 1.建立控制器 loginController.calss.php <?php namespace Admin\Controller; header('Content-type ...

  3. nutz中实现登录验证

    一.nutz是什么 nutz是一个轻便的web端开发框架.主页如下:http://www.nutzam.com/core/nutz_preface.html 二.session简单介绍 大家都知道ht ...

  4. 使用 FormsAuthentication 来进行登录验证

    1.在web.config文件中,<system.web>/<authentication>配置节用于对验证进行配置.为<authentication>节点提供mo ...

  5. Python_socket常见的方法、网络编程的安全注意事项、socketsever模块、浏览器中在一段时间记录用户的登录验证机制

    1.socket常见的方法 socket_常见方法_服务器端 import socket from socket import SOL_SOCKET,SO_REUSEADDR sk = socket. ...

  6. Django-rest_framework中利用jwt登录验证时,自定义返回凭证和登录校验支持手机号

    安装 pip install djangorestframework-jwt 在Django.settings中配置 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATIO ...

  7. Asp.net MVC访问框架页中嵌套的iframe页面时,如果session或cookie过期,登录验证超时怎样自动跳转到登录页

    一般登录验证的过滤器中,使用验证过滤器的Redirect方法,将请求重定向到指定的URL.但是如果我们要访问的页面是一个嵌套在框架页中的iframe页面时,这种重定向只会对iframe页面凑效,也就是 ...

  8. Redis 在java中的使用(登录验证,5分钟内连续输错3次密码,锁住帐号,半小时后解封)(三)

    在java中使用redis,做简单的登录帐号的验证,使用string类型,使用redis的过期时间功能 1.首先进行redis的jar包的引用,因为用的是springBoot,springBoot集成 ...

  9. Spring Security在登录验证中增加额外数据(如验证码)

    在使用Spring Security框架过程中,经常会有这样的需求,即在登录验证时,附带增加额外的数据,如验证码.用户类型等.下面将介绍如何实现. 注:我的工程是在Spring Boot框架基础上的, ...

随机推荐

  1. ESXi 与其它虚拟化底层产品之比较:

    序号 虚拟化管理程序属性 VMware ESXi 5.0 采用 Hyper-V 的 Windows Server 2008 R2 SP1 Citrix XenServer 5.6 FP1  1 磁盘占 ...

  2. How to interact with the Chef Server using the Chef Server API using Shell script

    !/usr/bin/env bash   _chef_dir () { # Helper function: # Recursive function that searches for chef c ...

  3. POJ2763-Housewife Wind(树链剖分)

    也是入门题,和上一题不一样的是权值在边上. 调了半天后来发现线段树写错了,build的时候没有pushup...蠢哭了好吗.... 做题还是不专心,太慢辣.. #include <algorit ...

  4. html5+css3中的background: -moz-linear-gradient 用法

    在CSS中background: -moz-linear-gradient 让网站背景渐变的属性,目前火狐3.6以上版本和google浏览器支持这个属性. background: -moz-linea ...

  5. (转)UML序列图总结

    序列图主要用于展示对象之间交互的顺序. 序列图将交互关系表示为一个二维图.纵向是时间轴,时间沿竖线向下延伸.横向轴代表了在协作中各独立对象的类元角色.类元角色用生命线表示.当对象存在时,角色用一条虚线 ...

  6. POJ 2502 Subway

    Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4928   Accepted: 1602 Descriptio ...

  7. Spring入门(2)-通过构造器注入Bean

    Spring入门(2)-通过构造器注入Bean 前一篇文章将了最基本的spring例子,这篇文章中,介绍一下带有参数的构造函数和通过构造器注入对象引用. 0. 目录 带有参数的构造函数 通过构造器注入 ...

  8. MySQL Update语句用法

    用一个表的某列值更新另外一个表的某列值的sql语句: update tableA a innner join tableB b on a.column_1 = b.column_1 set a.col ...

  9. Android子线程更新UI的方法总结

    版权声明:本文为博主原创文章,转载请注明出处:https://i.cnblogs.com/EditPosts.aspx?postid=6121280 消息机制,对于Android开发者来说,应该是非常 ...

  10. android知乎小圆圈刷新效果

    前几天看到包括知乎在内的很多应用都使用到了一种小圆圈划动刷新的效果,仔细查了一下,原来是android v7包里面最新的刷新效果.使用起来也是十分简单的. 首先先建立一个activity,在布局文件里 ...