惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Fortinet All Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
Recent Announcements
Recent Announcements
Jina AI
Jina AI
G
Google Developers Blog
腾讯CDC
博客园_首页
博客园 - 【当耐特】

博客园 - David.C

ASP.net 验证码(C#) 这是我最近从网上摘抄的.net笔试题 office应用小技巧 正确显示出Word中的空格 仓库管理系统解决方案 餐饮业点菜系统解决方案 餐饮服务案例分析(预订) Asp.Net在SqlServer中的图片存取技术 客户关系管理的三大纪律 软件工程---网站项目管理 电子政务与办公自动化初探 详解加密技术概念、加密方法以及应用 痛苦的选择:不再只专注于技术(转载--http://zhenyulu.cnblogs.com/archive/2004/10/17/53443.aspx) 在C#中调用Microsoft.VisualBasic命名空间下的类型验证函数 用C#实现基于用C#实现基于TCP协议的网络通讯 MSDN 文章收藏 数据访问:使用 ADO.NET 的最佳实践(ADO.NET 技术文档) ASP.NET编程中的十大技巧 用ASP.NET上传图片并生成可带版权信息的缩略图 VS.NET 2003 控件命名规范
Asp.Net安全验证小结 - David.C - 博客园
David.C · 2005-10-30 · via 博客园 - David.C

1,基于windows的安全验证
 web.config文件:
  <configuration>
    <system.web>
        <authentication mode="Windows" />
        <identity impersonate="true" />
        <authorization>
            <allow roles="BUILTIN\groupname" users="computername\UserName,computername\UserName" />
            <deny users="*" />
        </authorization>
    </system.web>
  </configuration>
  在.aspx文件中无需任何代码就可以实现验证,但可以在.aspx文件获取登陆用户的信息
  需导入命名空间:System.Security.Principal
  if(User.Identity.IsAuthenticated)//判断用户是否验证,似乎可有可无
  {
    WindowsIdentity objWinIdentity=WindowsIdentity.GetCurrent();
    lblHelloMsg.Text="the name:"+objWinIdentity.Name+"<br>Type:"+ objWinIdentity.AuthenticationType+"IsInRole:"+User.IsInRole("computername\\groupname");
  }
 
2,基于web.config forms验证
 web.config文件:
 <configuration>
<system.web>
  <authentication mode="Forms">
    <forms name="MyApp" path="/" loginUrl="login.aspx"
           protection="All" timeout="30">
      <credentials passwordFormat="Clear">
        <user name="kwk" password="test" />
        <user name="ljx" password="test" />
      </credentials>
    </forms>
  </authentication>

  <authorization>
    <allow users="kwk,ljx" />
    <deny users="?" />
  </authorization>
</system.web>
</configuration>
 login.aspx文件:需要提供两个文本框用于填写用户和密码(txtUsr,txtPwd),一个单选框判断是否永久保存
                还需要一个按钮控件则响应该button的代码如下:
void DoLogin(Object sender, EventArgs e)
{
   if(FormsAuthentication.Authenticate(txtUsr.Value,txtPwd.Value))
   {
       FormsAuthentication.RedirectFromLoginPage(txtUsr.Value,chkPersist.Checked);
   }
   else
   //为代码完整性而设置,可以不写
   {
       Response.Write("authentication fails");
   }

然后在别的页面可以获得登陆用户的值:
if(User.Identity.IsAuthenticated)//可以不需要判断
{
  Response.Write("your name:"+User.Identity.Name);
  Response.Write("验证类型:"+User.Identity.AuthenticationType);//forms,windows等
}

3,基于自定义forms验证
 web.config文件(基本上不需要什么设置):
  <system.web>
   <authentication mode="Forms">
  <forms name="MyApp" path="/" loginUrl="custom-login.aspx"
      protection="All"  timeout="30" >
  </forms>
   </authentication>

   <authorization>
  <deny users="?" />
   </authorization>
 </system.web>
  custom-login.aspx文件,基本原理还是跟2中说的一样,如:
  if (blnIsAuthenticated) //注意这个blnIsAuthenticated是一个自己定义的变量
  //当我们把用户输入的信息和数据库(或xml)的信息比对,存在则把该变量设为true,反之false
  //这是跟2不一样的地方
  {
     FormsAuthentication.RedirectFromLoginPage(txtUsr.Value, chkPersist.Checked);
     //txtUsr和chkPersist分别为textbox,checkbox控件
  }
  else
  {
    //验证失败提示信息
  }
  剩下的如在其他页面获得用户信息,如2一样
 
4,退出登陆
响应退出登陆按钮的代码:
FormsAuthentication.SignOut();
Response.Clear();
Response.Redirect(Request.UrlReferrer.ToString());//重定向到前一个页面