你调用的对象是空的

时间:2014-01-30 16:53:14

标签: c# asp.net forms-authentication basic-authentication

在我的asp web项目中。 我有3个页面命名为reg.aspx,log.aspxDefault.aspx。 在reg.aspx上,我希望用户自己进行自我监控。在reg页面上,我有2 textboxtextbox1textbox2button控制按钮1。

对于这个目的,我正在使用他们的用户名并传入web.config文件。 这是我的web.config代码

 <configuration>
        <system.web>
          <authentication mode="Forms">
            <forms loginUrl="Log.aspx" defaultUrl="Default.aspx">
              <credentials passwordFormat="MD5">
              </credentials>
            </forms>
          </authentication>
          <authorization>
            <deny users="?"/>
          </authorization>  
          <compilation debug="true" targetFramework="4.5" />
          <httpRuntime targetFramework="4.5" />
        </system.web>  
       <location path="Reg.aspx">
         <system.web>
           <authorization>
             <allow users="*"/>
           </authorization>
         </system.web>
       </location>
    </configuration>

然后点击reg页面的按钮,我已经编写了这段代码,

protected void Button1_Click(object sender, EventArgs e)
    {
        Configuration cfg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        ConfigurationSectionGroup cgs = cfg.SectionGroups["system.web"];
        AuthenticationSection Aus=(AuthenticationSection)cgs.Sections["Authentication"];
        Aus.Forms.Credentials.Users.Add(new FormsAuthenticationUser(TextBox1.Text,FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text,"MD5")));
        cfg.Save();
        Response.Write("Registration Success");

    }

但是当我尝试注册用户时,它会抛出null引用。我的代码中的问题在哪里。

0 个答案:

没有答案