`
luhai1992
  • 浏览: 58073 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

AspNet小结(用户权限)

阅读更多

AspNet 小结

一、主题皮肤

1.  设置页面的主题 Themes 属性

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"  Theme="SystemThemes"%>

2.  设置所有页面的主题

theme="SystemThemes"

<system.web>

    <pages theme="SystemThemes">

      <controls>

        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

      </controls>

    </pages>

3.  设置不同模块的主题

      <location path="Manager">

           <system.web>

                 <pages theme="OtherThemes"></pages>

                   </system.web>

         </location>

 

      注意path 路径不能有~/ 否则不能使用相应主题

二、 站点授权和用户权限 MemeberShip

 1 验证方式

                                     a) Form

b)Window 用于局域网,验证域名和计算机名

c)Passport 微软为企业设计(一般不用)

                     2.webConfig 配置      

                                     <system.web>下的

                               <authentication mode="Forms" />

                              注意: 如果建的是Ajax 网站是不会默认生成以上代码的

                    3. 网站管理工具

                            A 在项目的解决方案视图中含有一个配置图标点击可打开网站管理工具

                            B 可选择身份验证方式 系统会自动生成对应的库

注意:只有刷新App_Data才可看见生成的库

                            C 自动生成的表

a)        aspnet_Users 用户表

b)        aspnet_Roles 权限表

c)        aspnet_UsersInRoles 用户权限表

d)        aspnet_Membership 用户的其他信息

 

 

4. 配置文件

         A)

         Machine.config  配置服务器

一般路径,文件路径,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG

             设置验证信息格式:

                   membership 节点的所有内容copy webconfigsystem.web 节点下

                       <membership>

      <providers>

        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer"//连接字符串的名称

enablePasswordRetrieval="false" //密码是否支持找回

enablePasswordReset="true" //密码是否支持重置

requiresQuestionAndAnswer="true" //用户是否需要输入提示问题

//若设为true 可将注册控件的相关输入框去掉(需先变为可编辑的控件)

applicationName="/"  登陆页面的作用域

requiresUniqueEmail="false" //注册邮箱必须唯一

 passwordFormat="Hashed" //加密方式

//可以改成其他加密方式如DES但是需修改其提供程序

maxInvalidPasswordAttempts="5" //密码的最大尝试次数 0 代表无限次

minRequiredPasswordLength="7" 最小密码长度minRequiredNonalphanumericCharacters="1" //特殊字符数 没有写0

passwordAttemptWindow="10" //密码输入框的有效时间(单位:分钟)

 passwordStrengthRegularExpression="" 密码验证的正则表达式

/>

      </providers>

</membership>

解析:

     <providers> 设置提供程序

     connectionStringName="LocalSqlServer"//连接字符串的名称

<connectionStrings>

           <add name="CRMConnStr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|CRM.MDF;Integrated Security=True;User Instance=True"></add>

 

|DataDirectory| 代表项目中的App_Data 文件夹

 

</connectionStrings>

B) 更改配置文件的提供程序

         <add name="CRMSqlMembershipProvider"

//提供程序的名称可以自定义

type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ConStr"

             minRequiredPasswordLength="6"

             minRequiredNonalphanumericCharacters="0"

             requiresUniqueEmail="true"

             requiresQuestionAndAnswer="false" />

 

C) 设置Membership 的默认提供程序

         web.config system.web 下的membership 节点下的提供程序只有一个默认为此提供程序不用设定 defaultProvider 属性

<membership defaultProvider="CRMSqlMembershipProvider">

D ) 若自定义的提供程序和machine.config 中的提供程序的名称相同则必须remove 原有的提供程序

<remove name="AspNetSqlMembershipProvider"/>

5. 登陆控件

        a) 得到当前登陆的用户名

                   得到当前的用户名 LoginName 控件

b)         得到当前页面的登陆状态

LoginStatus 控件

c)         CreateUserVizard 控件

              //得到当前控件的制定步骤

         this.CreateUserWizard1.WizardSteps[0];

         this.CreateUserWizard1.WizardSteps[0].FindControl("");

        //得到当前控件的活动步骤

         this.CreateUserWizard1.ActiveStep;

        //得到当前控件的活动步骤的ID

  this.CreateUserWizard1.ActiveStepIndex;

                   常用属性:

                            DisableCreateUser=true; 注册成功后不能使用(禁用)

                            LoginCreatedUser=”false”注册成功后是否处于登陆状态

                            AutoGeneratePassword=”true” 是否自动生成密码

6. 后台得到注册控件的注册信息,和其他注册信息的内容

            //得到当前注册用户的用户名

        ViewState["User"]=this.CreateUserWizard1.UserName;

        //通过当前注册用户的用户名得到用户的ID

        MembershipUser user1 = Membership.GetUser(this.CreateUserWizard1.UserName);

        Guid userid=(Guid)user1.ProviderUserKey;

        //得到当前用户的用户真实姓名和电话

         String  userName=(this.CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtUserName") as TextBox).Text;

         String telephone = (this.CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtTelephone") as TextBox).Text;

7.后台创建用户

     Membership.CreateUser("用户名", "password", "email", "密码提示问题", true, MembershipCreateStatus.Success);

最后一个参数:一个 MembershipCreateStatus 值,该值指示该用户是否成功创建或用户创建失败的原因。(是一个输出类型的参数)

8 得到登陆用户的用户名

       User.identity.Name  User 的名称在web.config 文件中定义可以更改

9. 用户登陆

       a)登录方式和登录权限设定

       Web.config

<authentication mode="Forms">

<forms loginUrl="~/Login.aspx" name="User" defaultUrl="~/Manager/UserAdd.aspx"></forms>

                 //设定默认登录页面和登录后的跳转页面

           </authentication>

           <authorization>

                 <deny users="?"/>//?代表匿名用户

                 <allow users="*"/>//*代表所有用户

//设置登录权限 阻止匿名用户访问页面允许登录用户访问页面

           </authorization>

       b)解决阻止匿名用户访问页面后登录页面所引用的页面或样式无法显示问题

            

      <location path="validateCode.aspx">

           <system.web>

                 <authorization>

                      <allow users="?"/>

                      <allow users="*"/>

                 </authorization>

           </system.web>

      </location>

//将无限制的页面用location 标签配置 注意:此标签放在System.web 节点外

C)手动登录

       MemberShip.ValidateUser(用户名,密码);

d ) 得到当前登录的人数

       MemberShip.GetMemberShipOnline();

E ) 用户登录时显示验证码

1.       在登录界面中放置一个Image 控件

2.       Image控件的Url设置对应的验证图片页面路径

3.       编写验证页面,并将随机生成的验证码放到Session

  protected void Page_Load(object sender, EventArgs e)

    {

        System.Drawing.Bitmap image = new Bitmap(50, 30);

        Graphics graphics = Graphics.FromImage(image);

        graphics.Clear(Color.Yellow);

        StringBuilder sb = new StringBuilder();

        Random random = new Random();

        for (int i = 0; i < 4; i++)

        {

            sb.Append(random.Next(0, 10));

        }

        Session["ValidateCode"] = sb.ToString();

        Font font = new Font("宋体", 15, FontStyle.Bold | FontStyle.Italic);

 

        //渐变

        System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.White, 1.2f, true);

        graphics.DrawString(sb.ToString(), font, brush, 0, 0);

 

        //添加噪音线

        for (int i = 0; i < 3; i++)

        {

            Point p1 = new Point(random.Next(1, image.Width - 1), random.Next(1, image.Height - 1));

            Point p2 = new Point(random.Next(1, image.Width - 1), random.Next(1, image.Height - 1));

            graphics.DrawLine(Pens.Red, p1, p2);

        }

 

        //添加噪音点

        for (int i = 0; i < 50; i++)

        {

            image.SetPixel(random.Next(1, image.Width - 1), random.Next(1, image.Height - 1), colors[random.Next(0, colors.Length)]);

        }

 

        graphics.Save();

        Response.ContentType = "image/gif";

        image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

        graphics.Dispose();

text-align: left; text-indent: 0cm; margin:

0
0
分享到:
评论

相关推荐

    ASPNET35开发大全第一章

    1.6 小结 第2章 C# 3.0程序设计基础 2.1 C#程序 2.1.1 C#程序的结构 2.1.2 C# IDE的代码设置 2.2 变量 2.2.1 定义 2.2.2 值类型 2.2.3 引用类型 2.3 变量规则 2.3.1 命名规则和命名习惯 2.3.2 声明并初始化变量 ...

    IIS6.0的默认权限和用户权限设置小结

    ### IIS6.0的默认权限和用户权限设置详解 #### 一、引言 IIS (Internet Information Services) 是微软公司提供的一款Web服务组件,主要用于Windows操作系统上部署Web应用程序和服务。IIS 6.0作为Windows Server ...

    WindowsServer2003 + IIS6.0 + ASP + NET + PHP + PERL + MSSQL + MYSQL 最新服务器安全设置技术实例.doc

    #### 三、小结 通过对Windows Server 2003服务器上IIS 6.0、ASP、.NET、PHP、PERL、MSSQL和MySQL等组件的硬盘权限进行合理配置,可以有效提升服务器的整体安全性。上述权限设置旨在为不同场景提供参考,但在实际...

    asp.net期末课程设计

    7. **身份验证和授权**:实现用户登录、注册和权限管理,确保系统的安全性。 8. **异常处理和日志记录**:学习如何捕获和处理程序运行时可能出现的错误,以及记录和分析日志信息。 9. **响应式设计**:使网站适应...

    Asp.net调试的一些问题小结

    这时可以通过在本地用户和组管理中将aspnet用户添加到管理员组,从而赋予必要的权限。 此外,如果IIS设置不当,也会导致无法创建或打开应用程序。在本案例中,作者经过一系列的排查和尝试,包括重启机子和重新配置...

    EAI问题与解决思路汇总

    ### 小结 EAI在用友U8系统中的应用非常广泛,尤其是在零售分销、CRM等领域。然而,由于其复杂的配置和依赖关系,经常会遇到各种各样的问题。本文针对常见的EAI接口配置问题进行了详细的解析,并提供了具体的解决...

    ASP.NET对路径&quot;xxxxx&quot;的访问被拒绝的解决方法小结

    除此之外,网络上还存在其他可能的解决办法,例如重新安装ASP.NET、提高Everyone或ASPNET用户的权限、删除并重建IIS虚拟目录、在`web.config`中启用身份模拟(`&lt;identity impersonate="true" /&gt;`)等。每种方法都有其...

Global site tag (gtag.js) - Google Analytics