博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net form验证
阅读量:7201 次
发布时间:2019-06-29

本文共 2503 字,大约阅读时间需要 8 分钟。

借鉴别人的代码,自己整理下

有些代码不是很清楚,先记录一下

1.首先设置web.config

(1)把authentication的mode改为forms,原来默认的应该是windows,同时设置登录页面

<authentication mode="Forms">

  <forms loginUrl="login.aspx" name=".ASPXAUTH"></forms>
</authentication>

(2)设置权限目录

    location这个目录和system.web是同一级目录,path为设置权限的目录,即为指定该目录下文件的访问权限

<location path="User">

  <system.web>
    <authorization>
      <allow roles="user"/>
      <deny users="*"/>
    </authorization>
  </system.web>
</location>

(可以设置多个location,这样可以设置多个文件夹下目录的权限)

2 .登录页面设置

   (1)验证方法

/// <summary>

/// Set information for authentication
/// </summary>
/// <param name="username">Username</param>
/// <param name="roles">Roles</param>
private void SetAuthentication(string username, string roles)
{
  // Create a new ticket used for authentication
  FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
    1, // Ticket version
    username, // Username associated with ticket
    DateTime.Now, // Date/time issued
    DateTime.Now.AddMinutes(60), // Date/time to expire
    false, // "true" for a persistent user cookie
    roles, // User-data, in this case the roles
  FormsAuthentication.FormsCookiePath);// Path cookie valid for

  // Encrypt the cookie using the machine key for secure transport

  string hash = FormsAuthentication.Encrypt(ticket);
  HttpCookie cookie = new HttpCookie(
    FormsAuthentication.FormsCookieName, // Name of auth cookie
    hash); // Hashed ticket

  // Set the cookie's expiration time to the tickets expiration time

  if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

  // Add the cookie to the list for outgoing response

  HttpContext.Current.Response.Cookies.Add(cookie);
}

  (2)登录

   if(验证成功)

    SetAuthentication(uname, "user");//第一个为名字,第二个为角色

3.Global.asax设置

protected void Application_AuthenticateRequest(Object sender, EventArgs e)

{
  if (HttpContext.Current.User != null)
  {
    if (HttpContext.Current.User.Identity.IsAuthenticated)
    {
      if (HttpContext.Current.User.Identity is FormsIdentity)
      {
        FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
        FormsAuthenticationTicket ticket = id.Ticket;

        // Get the stored user-data, in this case, our roles

        string userData = ticket.UserData;
        string[] roles = userData.Split(',');
        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
      }
    }
  }
}

经测试可以使用,设置权限的文件夹下必须为允许的角色能够打开,起到了分文件夹权限的作用

暂时不知道优缺点。

转载于:https://www.cnblogs.com/m-cnblogs/archive/2012/09/27/2705612.html

你可能感兴趣的文章
Sping 核心IOC容器
查看>>
poj 2524
查看>>
MapReduce
查看>>
论文阅读笔记五十六:(ExtremeNet)Bottom-up Object Detection by Grouping Extreme and Center Points(CVPR2019)...
查看>>
回收期计算
查看>>
response响应
查看>>
10 个十分难得的 javascript 开发经验
查看>>
Common Subsequence
查看>>
【CSS3】标签使用说明
查看>>
n皇后问题—回溯法 C++实现
查看>>
on delete cascade
查看>>
Connected Graph
查看>>
openfile iscisi 配置
查看>>
SCAU 9504 面试
查看>>
基本数据类型、输入输出、运算符
查看>>
WuKong 最短路&&记忆化搜索
查看>>
Smart3D系列教程4之 《案例实战演练1——小物件的照片三维重建》
查看>>
C# 模拟多线程下载文件
查看>>
[17]CSS3 变形效果(上)
查看>>
JSP 脚本中的 9 个内置对象
查看>>