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. 优秀java开源项目与解决方案推荐与概论

    http://www.oschina.net/project/lang/19/java http://www.open-open.com/ http://java-source.net/ https: ...

  2. homework-04

    1.准备工作 本次结对编程我们对项目本身就行了分工,既然是测试来驱动开发,那么我们就把本次工作分成了测试与生成两个部分,小明同学负责生成测试数据,而我写测试程序检测测试结果是否正确,相对来说还是小明同 ...

  3. ocp 1Z0-047 1-60题解析

    1. You need to load information about new customers from the NEW_CUST table into the tables CUST and ...

  4. HDU 5791 Two (DP)

    Two 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5791 Description Alice gets two sequences A and ...

  5. POJ Wormholes (SPFA)

    http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...

  6. Linux下c/c++项目代码覆盖率的产生方法

    最近做了一系列的单元测试相关的工作,除了各种规范及测试框架以外,讨论比较多的就是关于代码覆盖率的产生,c/c++与其他的一些高级语言或者脚本语言相比较而言,例如 Java..Net和php/pytho ...

  7. Schwarz导数与凹凸性

    命题 1: 定义区间$I$上的Schwarz导数$$D^{2}f(x)=\lim_{h\to 0}\frac{f(x+h)+f(x-h)-2f(x)}{h^{2}}$$若$D^{2}f(x)\geq ...

  8. sql prompt格式设置

    sql prompt格式设置. 格式前: 格式后:

  9. cocos2d-x CCListView

    转自:http://blog.csdn.net/onerain88/article/details/7641126 cocos2d-x 2.0 版更新了,把opengl 1.1 替换为opengl 2 ...

  10. Linux - 打印文件夹全部文件 代码(C)

    列出文件夹全部文件 代码(C) 本文地址:http://blog.csdn.net/caroline_wendy 首先配置环境,參考:http://blog.csdn.net/caroline_wen ...