配置项描述:

<authentication mode="Forms">

<forms

name=".ASPXAUTH"

loginUrl="login.aspx"

defaultUrl="default.aspx"

protection="All"

timeout="30"

path="/"

requireSSL="false"

slidingExpiration="false"

enableCrossAppRedirects="false"

cookieless="UseDeviceProfile"

domain=""

/>

</authentication>

  以上代码使用的均是默认设置,换言之,如果你的哪项配置属性与上述代码一致,则可以省略该属性例如<forms name="MyAppAuth" />。下面依次介绍一下各种属性:

l  name——Cookie的名字。Forms Authentication可能会在验证后将用户凭证放在Cookie中,name属性决定了该Cookie的名字。通过FormsAuthentication.FormsCookieName属性可以得到该配置值(稍后介绍FromsAuthentication类)。

l  loginUrl——登录页的URL。通过FormsAuthentication.LoginUrl属性可以得到该配置值。当调用FormsAuthentication.RedirectToLoginPage()方法时,客户端请求将被重定向到该属性所指定的页面。loginUrl的默认值为“login.aspx”,这表明即便不提供该属性值,ASP.NET也会尝试到站点根目录下寻找名为login.aspx的页面。

l  defaultUrl——默认页的URL。通过FormsAuthentication.DefaultUrl属性得到该配置值。

l  protection——Cookie的保护模式,可取值包括All(同时进行加密和数据验证)、Encryption(仅加密)、Validation(仅进行数据验证)和None。为了安全,该属性通常从不设置为None。

l  timeout——Cookie的过期时间。

l  path——Cookie的路径。可以通过FormsAuthentication.FormsCookiePath属性得到该配置值。

l  requireSSL——在进行Forms Authentication时,与服务器交互是否要求使用SSL。可以通过FormsAuthentication.RequireSSL属性得到该配置值。

l  slidingExpiration——是否启用“弹性过期时间”,如果该属性设置为false,从首次验证之后过timeout时间后Cookie即过期;如果该属性为true,则从上次请求该开始过timeout时间才过期,这意味着,在首次验证后,如果保证每timeout时间内至少发送一个请求,则Cookie将永远不会过期。通过FormsAuthentication.SlidingExpiration属性可以得到该配置值。

l  enableCrossAppRedirects——是否可以将以进行了身份验证的用户重定向到其他应用程序中。通过FormsAuthentication.EnableCrossAppRedirects属性可以得到该配置值。为了安全考虑,通常总是将该属性设置为false。

l  cookieless——定义是否使用Cookie以及Cookie的行为。Forms Authentication可以采用两种方式在会话中保存用户凭据信息,一种是使用Cookie,即将用户凭据记录到Cookie中,每次发送请求时浏览器都会将该Cookie提供给服务器。另一种方式是使用URI,即将用户凭据当作URL中额外的查询字符串传递给服务器。该属性有四种取值——UseCookies(无论何时都使用Cookie)、UseUri(从不使用Cookie,仅使用URI)、AutoDetect(检测设备和浏览器,只有当设备支持Cookie并且在浏览器中启用了Cookie时才使用Cookie)和UseDeviceProfile(只检测设备,只要设备支持Cookie不管浏览器是否支持,都是用Cookie)。通过FormsAuthentication.CookieMode属性可以得到该配置值。通过FormsAuthentication.CookiesSupported属性可以得到对于当前请求是否使用Cookie传递用户凭证。

l  domain——Cookie的域。通过FormsAuthentication.CookieDomain属性可以得到该配置值。

登录代码:


[HttpPost] public ActionResult LogOn(LogOnModel model) {   if (ModelState.IsValid)   {
    if (model.UserName == "username" && model.Password == "password")     {       FormsAuthentication.RedirectFromLoginPage(model.UserName, model.RememberMe);     }   }
  return View("logon failed!"); }

退出代码:


public ActionResult LogOff() {       //FormsService.SignOut();       FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home"); }

FormsAuthentication实现登录(转)的更多相关文章

  1. FormsAuthentication.RedirectFromLoginPage 登录

    首先讲的这个东西是针对后台数据访问的比如我的后台是admin文件夹.那么我除非登陆成功才可以访问里面的东西.那么除了Session对象判断.和Cookies来判断还能用到FormsAuthentica ...

  2. FormsAuthentication 登录兼容 IE11 保存cookie

    现象:使用FormsAuthentication进行登录验证,在IE11客户端无法保存cookie 解决方法:在web.config中的forms中增加cookieless="UseCook ...

  3. 总结FormsAuthentication的使用

    一.先看一下使用FormsAuthentication做登录认证的用法 用法一: FormsAuthentication.SetAuthCookie(username, isPersistent); ...

  4. FormsAuthentication知多少

    前述:对于FormsAuthentication相信大家都烂熟于胸了,这里只是做一下小结. 一.先看一下使用FormsAuthentication做登录认证的用法 用法一: FormsAuthenti ...

  5. 也谈Asp.net 中的身份验证

    钱李峰 的这篇博文<Asp.net中的认证与授权>已对Asp.net 中的身份验证进行了不错实践.而我这篇博文,是从初学者的角度补充了一些基础的概念,以便能有个清晰的认识. 一.配置安全身 ...

  6. ASP.NET CORE中使用Cookie身份认证

    大家在使用ASP.NET的时候一定都用过FormsAuthentication做登录用户的身份认证,FormsAuthentication的核心就是Cookie,ASP.NET会将用户名存储在Cook ...

  7. Form身份验证

    Forms身份验证Web.config<system.web><authentication mode="Forms">            <fo ...

  8. [转]Asp.Net大型项目实践(11)-基于MVC Action粒度的权限管理【续】【源码在这里】(在线demo,全部源码)

    本文转自:http://www.cnblogs.com/legendxian/archive/2010/01/25/1655551.html 接上篇Asp.Net大型项目实践(10)-基于MVC Ac ...

  9. Cookie-based认证实现

    (使用ASP.NET 4.X 中的时候一定都用过Forms认证即FormsAuthentication做登录用户的身份认证,FormsAuthentication的核心就是Cookie,ASP.NET ...

随机推荐

  1. Integer和String "+""=="方法的不同

    在上面的两个篇博客中,我们看到String和Integer不同的常量池的变现形式 我们再看一个例子: public static void main(String[] args) { // TODO ...

  2. Linux 下几个重要目录解释

    linux下把所有的东西都当作文件的,,一个分区想要使用就需要挂载到一个文件夹上,那这个文件夹就叫挂载点,像常用的把 设备/dev/cdrom挂载到/mnt,,,你想问的应该是linux的各目录的解释 ...

  3. Shapefile点图层转换为Shapefile线图层

    在Oracle数据表转换为Shapefile(一)和Oracle数据表转换为Shapefile(二)两篇文章中,分别介绍了两种不同的根据Oracle数据表生成Shapefile点图层的方法.本文在此基 ...

  4. 双系统Grub引导下恢复windows引导的方法

    此方法适用于windows系统正常,linux和windows 双系统下恢复windows系统引导.需要使用windows安装u盘 1. 启动至windows安装u盘,点击修复计算机 2. 进入命令行 ...

  5. Backup--批量备份和还原

    -----------------------------批量备份数据------------------------------------------- Use master GO /*===== ...

  6. solr的schema.xml配置属性解释

    schema.xml做什么? SOLR加载数据,创建索引和数据时,核心数据结构的配置文件是schema.xml,该配置文件主要用于配置数据源,字段类型定义,搜索类型定义等.schema.xml的配置直 ...

  7. php获取数组的键值

    <?php header ( "Content-type: text/html; charset=utf-8" ); $info[; $info[; $info[; $inf ...

  8. C#多线程编程实战1.1创建线程

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  9. GEEK UNINSTALLER

    geek.exe https://geekuninstaller.com/download Event log message indicates that the Windows Installer ...

  10. lua之base64加密和解密算法。

    local function encodeBase64(source_str) local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop ...