zoukankan      html  css  js  c++  java
  • MVC的Forms登录验证

    第一步:修改web.config配置

    在 <system.web>节点下加入配置:未登录的用户跳转到/Home/Login,登录后跳转到/Home/UserCenter,登录后票证记录到cookie,cookie超时时间2880分钟。<allow users="?" />是允许匿名访问的意思。

    <authentication mode="Forms" >
          <forms defaultUrl="/Home/UserCenter" loginUrl="/Home/Login" timeout="2880"></forms>
        </authentication>
        <authorization>
          <allow users="?"/>
        </authorization>

    forms配置说明:

    <forms
    name="name"
    loginUrl="URL"
    defaultUrl="URL"
    protection="[All|None|Encryption|Validation]"
    timeout="[MM]"
    path="path"
    requireSSL="[true|false]"
    slidingExpiration="[true|false]">
    enableCrossAppRedirects="[true|false]"
    cookieless="[UseUri|UseCookie|AutoDetect|UseDeviceProfile]"
    domain="domain name"
    ticketCompatibilityMode="[Framework20|Framework40]">
    <credentials>...</credentials>
    </forms>

    name:指定要用于身份验证的 HTTP Cookie。如果正在一台服务器上运行多个应用程序并且每个应用程序都需要唯一的 Cookie,则必须在每个应用程序的 Web.config 文件中配置 Cookie 名称。默认值为 ".ASPXAUTH"。
    loginUrl:指定如果找不到任何有效的身份验证 Cookie,将请求重定向到的用于登录的 URL。默认值为 login.aspx。
    defaultUrl:定义在身份验证之后用于重定向的默认 URL。默认值为 "default.aspx"。
    protection:指定 Cookie 使用的加密类型(如果有)。默认值为 All。
    timeout:指定 Cookie 过期前逝去的时间(以整数分钟为单位)。如果 SlidingExpiration 属性为 true,则 timeout 属性是滑动值,会在接收到上一个请求之后的指定时间(以分钟为单位)后过期。 为防止危及性能并避免向开启 Cookie 警告的用户发出多个浏览器警告,当指定的时间逝去大半时将更新 Cookie。这可能导致精确性受损。默认值为 "30"(30 分钟)。
    path:为应用程序发出的 Cookie 指定路径。默认值是斜杠 ( /),这是因为大多数浏览器是区分大小写的,如果路径大小写不匹配,浏览器不会送回 Cookie。
    requireSSL:指定是否需要 SSL 连接来传输身份验证 Cookie。默认值为 False。
    slidingExpiration:指定是否启用可调过期时间。可调过期将 Cookie 的当前身份验证时间重置为在单个会话期间收到每个请求时过期。默认值为 True。
    enableCrossAppRedirects:表明是否将通过身份验证的用户重定向到其他 Web 应用程序中的 URL。默认值为 False。
    cookieless:定义是否使用 Cookie 以及 Cookie 的行为。默认值为 UseDeviceProfile.
    domain:指定在传出 Forms 身份验证 Cookie 中设置的可选域。此设置的优先级高于 httpCookies 元素中使用的域。默认值为空字符串 ("")。
    ticketCompatibilityMode:指定在 Forms 身份验证中对于票证到期日期使用协调世界时 (UTC) 还是本地时间。默认值为 Framework20。
    子元素 credentials:允许选择在配置文件中定义名称和密码凭据。您还可以实现自定义的密码架构,以使用外部源(如数据库)来控制验证。

    第二步:web.config中的 <system.webServer>下的<remove name="FormsAuthentication" />这一行删掉,或注释掉。

    第三步:用户登录,记录票证

    [HttpPost]
            public ActionResult Login(SampleUser data)
            {
                SampleUser su = SampleUser.GetUser(data.UserName);//数据库查询用户
                if (su != null)
                {
                    //如果为 true,则创建持久 Cookie(跨浏览器会话保存的 Cookie)
                    //添加票据,并将用户导航到默认页面/Home/UserCenter
                    FormsAuthentication.RedirectFromLoginPage(data.UserName, true);
                    //仅添加票据,不做其他动作
                    //FormsAuthentication.SetAuthCookie(data.UserName, true);
                }
                return View();
            }

    第四步:验证用户票据

    [Authorize]
            public ActionResult UserCenter()
            {
                SampleUser user = FormsHelper.IsOk();//自行判断
                if (user != null)
                {
                    return View(user);
                }
                return RedirectToAction("Login");
            }

    FormsHelper是自己编码判断:需要引用System.Web命名空间

    public class FormsHelper
        {
            public static SampleUser IsOk()
            {
                if (HttpContext.Current.User != null && HttpContext.Current.User.Identity != null && HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    SampleUser user = SampleUser.GetUser(HttpContext.Current.User.Identity.Name);
                    if (user != null)
                    {
                        return user;
                    }
                }
                return null;
            }
        }

    在Action上面标记[Authorize],或者自行判断,都可以验证用户是否合法。在Action中也可以直接写User.Identity,不过命名空间就不一样了(User就是System.Web.MVC下的) 。

    第五步:退出登录

    public ActionResult SignOut()
            {
                FormsAuthentication.SignOut();
                return Redirect(FormsAuthentication.LoginUrl);
            }
  • 相关阅读:
    织梦开发——相关阅读likeart应用
    织梦标签教程
    织梦专题调用代码
    HIT 2543 Stone IV
    POJ 3680 Intervals
    HIT 2739 The Chinese Postman Problem
    POJ 1273 Drainage Ditches
    POJ 2455 Secret Milking Machine
    SPOJ 371 Boxes
    HIT 2715 Matrix3
  • 原文地址:https://www.cnblogs.com/xsj1989/p/8036098.html
Copyright © 2011-2022 走看看