FormsAuthentication实现登录

 

FormsAuthentication实现登录

配置项描述:

<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 " ); }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
单点登录(Single Sign-On,SSO)是指用户只需登录一次,即可在多个应用系统中访问被授权的资源,而无需再次登录或提供身份验证信息。在 C# 中,可以通过以下步骤实现单点登录: 1. 配置身份认证和授权。 在 Web.config 文件中,配置身份认证和授权,例如: ``` <system.web> <authentication mode="Forms"> <forms name=".ASPXAUTH" loginUrl="~/Account/Login" timeout="2880" /> </authentication> <authorization> <deny users="?" /> </authorization> </system.web> ``` 2. 实现登录面。 在登录面中,用户输入用户名和密码,然后将其传递到服务器进行验证。如果验证通过,则使用 FormsAuthentication.SetAuthCookie 方法创建认证票证,并跳转到主。 ``` if (IsValid(username, password)) { FormsAuthentication.SetAuthCookie(username, false); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", "The user name or password is incorrect."); return View(); } ``` 3. 在其他应用系统中验证认证票证。 其他应用系统可以通过检查认证票证来验证用户的身份。可以使用 FormsAuthentication.Decrypt 方法解密认证票证,然后检查票证是否过期或是否被篡改。 ``` HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); if (authTicket != null && !authTicket.Expired && authTicket.Name == username) { // User is authenticated } } ``` 以上是单点登录的基本实现步骤,但具体实现还需要根据实际需求进行调整和优化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值