有没有办法让管理员更改存储在凭证标签中的用户名和密码? [英] Is there any way to make a form for Admin to change his username and password stored in credential tag?

查看:73
本文介绍了有没有办法让管理员更改存储在凭证标签中的用户名和密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在web.config文件中使用了凭证标记来验证我的管理员。现在我想知道是否有任何方法可以创建一个带有管理员表单的页面来更改他的用户名和密码?这是我在web.config中的代码:

 <   system.web  >  
< 编译 debug = true targetFramework < span class =code-keyword> = 4.0 / >
< sessionState cookieless = AutoDetect mode = InProc timeout = 114400 >
< span class =code-keyword>< / sessionState >
< 身份验证 mode = 表单 >
< 表格 timeout = 1440 loginUrl = 〜/ entrance_before_paying.aspx defaultUrl = Admin / Default.aspx name = .ASPXFORMSDEMO cookieless = AutoDetect 保护 = 全部 >
< 凭据 passwordFormat = 清除 >
< 用户 名称 = elmiragolshanff@yahoo.com password = elmira / >
< /凭证 >
< / forms >
< / authentication >
< / system.web > ;



我在登录页面中使用此代码来检查用户是否是管理员:

 私人  void 输入()
{
if (FormsAuthentication.Authenticate(TextBox1.Text.Trim(),TextBox2.Text.Trim()))
{
FormsAuthentication.RedirectFromLoginPage( 凭证标记中的管理员用户名 true ) ;
}
其他
{
// < span class =code-comment>以用户身份输入}
}
}

解决方案

< blockquote> ChangePassword.aspx

< form ID =   form1 runat =   server  >  
< div>
< asp:TextBox ID = txtUsername runat = server >
< / asp:TextBox >
< ; asp:TextBox ID = txtPassword runat = server >
< / asp:TextBox >
< asp:按钮ID = btnWrite runat = < span class =code-string> server
onclick = btnWrite_Click Text = 修改 />
< / div >
< / 表格 >



ChangePassword.aspx.cs

  protected   void  btnWrite_Click( object  sender,EventArgs e)
{
配置webconfig = WebConfigurationManager.OpenWebConfiguration( );
SystemWebSectionGroup sysweb =(SystemWebSectionGroup)webconfig.GetSectionGroup( system.web);
AuthenticationSection authSection = sysweb.Authentication;
FormsAuthenticationUserCollection users = authSection.Forms.Credentials.Users;
FormsAuthenticationUser user = users [ 0 ];
user.Name = txtUsername.Text;
user.Password = txtPassword.Text;
webconfig.Save();
}



注意:确保向web.config文件及其父文件夹授予写入权限,否则您将获得异常

System.InvalidOperationException:锁定时无法编辑ConfigurationSection属性


I have used the credential tag in web.config file to authenticate my admin. now I want to know is there any way to make a page with a form for admin to change his username and password? this is my code in web.config:

<system.web>
    <compilation debug="true" targetFramework="4.0" />
    <sessionState cookieless="AutoDetect" mode="InProc"  timeout="114400">
    </sessionState>
    <authentication mode="Forms">
        <forms timeout="1440" loginUrl="~/entrance_before_paying.aspx" defaultUrl="Admin/Default.aspx" name=".ASPXFORMSDEMO"  cookieless="AutoDetect" protection="All">
            <credentials passwordFormat="Clear">
                <user name="elmiragolshanff@yahoo.com"  password="elmira" />
            </credentials>
        </forms>
    </authentication>
</system.web>


I used this code in my sign in page to check if the user is admin or not:

 private void enter()
     {
    if (FormsAuthentication.Authenticate(TextBox1.Text.Trim(), TextBox2.Text.Trim()))
    {
        FormsAuthentication.RedirectFromLoginPage("admin user name in credential tag", true);
    }
    else
    {
       // enter as a user}
    }
}

解决方案

ChangePassword.aspx

<form ID="form1"  runat="server">
    <div>
        <asp:TextBox ID="txtUsername" runat="server">
        </asp:TextBox>
        <asp:TextBox ID="txtPassword" runat="server">
        </asp:TextBox>
        <asp:Button ID="btnWrite" runat="server" onclick="btnWrite_Click" Text="Modify" />
    </div>
</form>


ChangePassword.aspx.cs

protected void btnWrite_Click(object sender, EventArgs e)
{
        Configuration webconfig = WebConfigurationManager.OpenWebConfiguration("~");
        SystemWebSectionGroup sysweb = (SystemWebSectionGroup)webconfig.GetSectionGroup("system.web");
        AuthenticationSection authSection = sysweb.Authentication;
        FormsAuthenticationUserCollection users = authSection.Forms.Credentials.Users;
        FormsAuthenticationUser user = users[0];
        user.Name = txtUsername.Text;
        user.Password = txtPassword.Text;
        webconfig.Save();
}


Note: Make sure to grant write permissions to web.config file and its parent folders otherwise you will get an exception "

System.InvalidOperationException: ConfigurationSection properties cannot be edited when locked


这篇关于有没有办法让管理员更改存储在凭证标签中的用户名和密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆