当前位置: 移动技术网 > IT编程>开发语言>.net > .net MVC中使用forms验证详解

.net MVC中使用forms验证详解

2017年12月08日  | 移动技术网IT编程  | 我要评论

丝袜空姐,毒胶囊企业名单,1733火影疾风坛

.net mvc中使用forms验证,供大家参考,具体内容如下

文件夹的分部是这样子的

首先在web.config中设置

authentication和authorization 节点

 <system.web>
  <authentication mode="forms">
   <forms loginurl="~/login/index" timeout="2880" defaulturl="~/home/index"/>
  </authentication>
  <anonymousidentification enabled="true"/>
  <authorization>
   <deny users="?"/> <!--拒绝匿名访问-->
  </authorization>
  <compilation debug="true" targetframework="4.5" />
  <httpruntime targetframework="4.5" />
  <httpmodules>
   <add name="applicationinsightswebtracking" type="microsoft.applicationinsights.web.applicationinsightshttpmodule, microsoft.ai.web" />
  </httpmodules>
 </system.web>

如果在login文件夹还有不需要匿名访问,或者在logincontroller中除了登陆还有方法可以匿名访问,

那么我们需要在加上这一个节点

 <location path="login"> <!--这里的意思就是logincontroller下的方法可以匿名访问-->
  <system.web>
   <authorization>
    <allow users="*" /> <!--允许匿名访问-->
   </authorization>
  </system.web>
 </location>

登陆的方法贴出一部分代码,仅供参考

 public bool validateuser(loginvo model)
    {
      string encodepassword = md5(model.password);//加密
      string sql =
        "select * from user_users where (username=@username or jobnumber=@jobnumber) and password=@password";
      var user = context.data.query<userspo>(sql,
        new {username = model.loginname, jobnumber = model.loginname, password = encodepassword}).singleordefault();
      if (user == null) return false;
      datetime expiration = model.isrememberlogin //是否记住密码
        ? datetime.now.adddays(14)
        : datetime.now.add(formsauthentication.timeout);
      var ticket=new formsauthenticationticket(
        1,//指定版本号:可随意指定
        user.username,//登录用户名:对应 web.config 中 <allow users="admin" … /> 的 users 属性
        datetime.now, //发布时间
        expiration,//失效时间
        true,//是否为持久 cookie
        user.userid.tostring(), //用户数据:可用 ((system.web.security.formsidentity)(httpcontext.current.user.identity)).ticket.userdata 获取
        formsauthentication.formscookiepath //指定 cookie 为 web.config 中 <forms path="/" … /> path 属性,不指定则默认为“/”
        );
      var encryptedticket = formsauthentication.encrypt(ticket);
      if (httpcontext.current.request.cookies[formsauthentication.formscookiename] != null)
      {
        httpcontext.current.request.cookies.remove(formsauthentication.formscookiename);
      }
      var loginidentify=new httpcookie(formsauthentication.formscookiename);
      if (model.isrememberlogin)
      {
        loginidentify.expires = datetime.now.adddays(7);
      }
      loginidentify.value = encryptedticket;
      httpcontext.current.response.appendcookie(loginidentify);//添加cookie
      return true;
    }

    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="str"></param>
    /// <param name="encoding"></param>
    /// <param name="toupper"></param>
    /// <param name="isreverse"></param>
    /// <param name="count"></param>
    /// <returns></returns>
    private string md5(string str, encoding encoding=null, int count = 1)
    {
      if (encoding == null)
      {
        encoding = encoding.default;
      }
      var bytes = new md5cryptoserviceprovider().computehash(encoding.getbytes(str));
      var md5 = string.empty;
      for (int i = 0; i < bytes.length; i++)
      {
        md5 += bytes[i].tostring("x").padleft(2, '0');
      }     
      if (count <= 1) { return md5; }
      return md5(md5, encoding, --count);
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网