如何在另一个页面ASP.NET中使用文本框值 [英] How to use textbox value in another page ASP.NET

查看:399
本文介绍了如何在另一个页面ASP.NET中使用文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码就在这里:

<%@ Page language =c#AutoEventWireup =true%>

<%@ Import Namespace = FormsAuth%>



 <%@     Page    语言  =  c#    AutoEventWireup   =  true < span class =code-attribute>  %>  
<%@ 导入 命名空间 = FormsAuth %>

< html >
< head >
< title > 登录< / title >
< / head >
< body align = center >
< 表格 id = 登录 方法 = 发布 runat = server >
< table cellpadding = 1 cellspacing = 1 style = background-color:#E0E0E0; border:1px solid#727272 align = center >
< h1 > PT.Packet Systems Indonesia < br / > 登录表单< / h1 >
< p > 请在此表单中输入您的帐户!< / p >
< tr >
< ; td >
< asp:标签 ID = Label1 Runat = 服务器 > 域名:< / asp:Label >

< / td >
< td >
< asp:TextBox ID = txtDomain Runat = 服务器 > < / asp:TextBox >
< / td > ;
< / tr >
< tr >
< td >
< asp:Label ID = Label2 Runat = 服务器 > 用户名: < / asp:标签 >
< / td >
< td >
< asp:TextBox ID = txtUsername Runat = 服务器 > < / asp :TextBox >
< / td >
< / tr >
< tr >
< td >
< asp:标签 ID = Label3 Runat = 服务器 > 密码:< / asp:Label >
< / td >
< td >
< asp:TextBox ID = txtPassword < span class =code-attribute> Runat = 服务器 TextMode = 密码 > ; < / asp:TextBox > ;
< / td >
< / tr >
< tr >
< td colspan = 2 >
< asp:CheckBox ID = chkPersist Runat = 服务器 文字 = Persist Cookie / >
< / td >
< ; / tr >
< tr >
< td colspan = 2 align = center >
< asp:按钮 ID =
btnLogin Runat = 服务器 文字 = 登录 OnClick = Login_Click > < / asp:按钮 >
< / td >
< / tr > ;
< tr >
< td colspan = 2 >
< asp:标签 ID = errorLabel Runat = 服务器 ForeColor = #ff3300 > < / asp:Label >
< / td >
< / tr < span class =code-keyword>>
< / table >
< / form >
< /正文 >
< / html >
< script runat = 服务器 >
void Login_Click( object sender,EventArgs e)
{
string adPath = LDAP://erpad-tbs-1.packet-systems.com; // LDAP目录服务器的路径
LdapAuthentication adAuth = LdapAuthentication(adPath);
尝试
{
if true == adAuth.IsAuthenticated(txtDomain.Text,txtUsername.Text,txtPassword.Text))
{
// 字符串组= adAuth.GetGroups();
字符串 groups = txtUsername.Text ;
// 创建故障单,然后添加组。
bool isCookiePersistent = chkPersist.Checked;
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1
txtUsername.Text,DateTime。现在,DateTime.Now.AddMinutes( 60 ),isCookiePersistent,groups);

// 加密票证。
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

// 创建一个cookie,然后将加密的票证作为数据添加到cookie中。
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket);

if true == isCookiePersistent)
authCookie.Expires = authTicket.Expiration;

// 将Cookie添加到传出Cookie集合中。
Response.Cookies.Add(authCookie);

// 您现在可以重定向。
Response.Redirect (FormsAuthentication.GetRedirectUrl(txtUsername.Text, false ));
}
else
{
errorLabel.Text = 验证失败。检查用户名和密码。;
}
}
catch (例外情况)
{
errorLabel.Text = 错误验证。 + ex.Message;
}
}
< / script >



在该代码中我有域名,用户名和密码TextBox。它们将在ldap中进行身份验证。



这里是我的ldapauthentication.cs代码:

 使用系统; 
使用 System.Text;
使用 System.Collections;
使用 System.DirectoryServices;
使用 System.DirectoryServices.AccountManagement;
使用 System.DirectoryServices.ActiveDirectory;

命名空间 FormsAuth
{
public class LdapAuthentication
{
private string _path;
private string _filterAttribute;

public LdapAuthentication( string path)
{
_path = path;
}

public bool IsAuthenticated( string domain, string username, string pwd)
{
string _domain = domain;
string domainAndUsername = domain + @ \\ \\ +用户名;
DirectoryEntry entry = new DirectoryEntry(_path,domainAndUsername,pwd);

尝试
{
// 绑定到原生AdsObject以强制进行身份验证。
object obj = entry.NativeObject;

DirectorySearcher search = new DirectorySearcher(条目);

search.Filter = (SAMAccountName = + username + ;
search.PropertiesToLoad.Add( cn);
SearchResult result = search.FindOne();

if null == result)
{
返回 false ;
}

// 更新目录中用户的新路径。
_path = result.Path;
_filterAttribute =( string )result.Properties [ cn] [ 0 ];
}
catch (例外情况)
{
throw new 异常( 验证用户时出错。 + ex.Message);
}

返回 true ;
}

public string GetGroups()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = (cn = + _filterAttribute + ;
search.PropertiesToLoad.Add( memberOf);
StringBuilder groupNames = new StringBuilder();

尝试
{
SearchResult result = search.FindOne();
int propertyCount = result.Properties [ 成员]计数。
string dn;
int equalsIndex,commaIndex;

for int propertyCounter = 0 ; propertyCounter < propertyCount; propertyCounter ++)
{
dn =( string )result.Properties [ memberOf] [propertyCounter];
equalsIndex = dn.IndexOf( = 1 );
commaIndex = dn.IndexOf( 1 );
if ( - 1 == equalsIndex)
{
return null ;
}
groupNames.Append(dn.Substring((equalsIndex + 1 ),(commaIndex - equalsIndex) - 1 ));
groupNames.Append( |);
}
}
catch (例外情况)
{
throw new 例外( 错误获取组名。 + ex.Message);
}
return groupNames.ToString();
}


}
}



如何使用登录表单的值来更改密码表格?

i想要更改密码。



我尝试了什么:



我尝试制作新参数,但我仍然感到困惑。

我只想取旧密码的价值并将其更改为新密码。

有人可以帮助我吗?

解决方案

你可以在Session中存储旧密码。其他选项是使用Profile Provider或者创建一个包含登录后所需信息的自定义类。您还可以查看ASP.NET标识以添加自定义声明。但出于安全考虑,我宁愿让用户输入旧密码来更改新密码。


我建​​议您必须要求旧密码更改密码

另外明智的是,如果你只是想用登录来改变它,那么你需要使用Section

基本上是将数据从一个表单传递给另一个表单你可以在任何地方调用session



在会话变量中添加值



 Session.Add(   Programe,ddlProgamme.SelectedValue); 



程序是会话变量的名称

它的数据类型默认为字符串



从会话变量中获取值

  .program =( string )(会话[  Programe ]) ; 


I have code right here:
<%@ Page language="c#" AutoEventWireup="true" %>
<%@ Import Namespace="FormsAuth" %>

<%@ Page language="c#" AutoEventWireup="true" %>
<%@ Import Namespace="FormsAuth" %>

<html>
<head>
    <title>Login</title>
</head>
  <body  align="center">
    <form id="Login" method="post" runat="server">
    <table cellpadding="1" cellspacing="1" style="background-color: #E0E0E0; border: 1px solid #727272"  align="center">
      <h1>PT. Packet Systems Indonesia <br/> Login Form</h1>
      <p>Please enter your account in this form!</p>
      <tr>
        <td>
            <asp:Label ID="Label1" Runat=server >Domain:</asp:Label>
      
        </td>
        <td>
            <asp:TextBox ID="txtDomain" Runat=server ></asp:TextBox>
        </td>
      </tr>
      <tr>
        <td>
            <asp:Label ID="Label2" Runat=server >Username:</asp:Label>
        </td>
        <td>
            <asp:TextBox ID=txtUsername Runat=server ></asp:TextBox>
        </td>
      </tr>
      <tr>
        <td>
            <asp:Label ID="Label3" Runat=server >Password:</asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtPassword" Runat=server TextMode=Password></asp:TextBox>
        </td>
      </tr>
      <tr>
        <td colspan="2">
            <asp:CheckBox ID=chkPersist Runat=server Text="Persist Cookie" />
        </td>
      </tr>
      <tr>
        <td colspan="2" align="center">
            <asp:Button ID="btnLogin" Runat=server Text="Login" OnClick="Login_Click"></asp:Button>
        </td>
      </tr>
      <tr>
        <td colspan="2">
            <asp:Label ID="errorLabel" Runat=server ForeColor=#ff3300></asp:Label>
        </td>
      </tr>
    </table>
    </form>
  </body>
</html>
<script runat=server>
void Login_Click(object sender, EventArgs e)
{
  string adPath = "LDAP://erpad-tbs-1.packet-systems.com"; //Path to your LDAP directory server
  LdapAuthentication adAuth = new LdapAuthentication(adPath);
  try
  {
    if(true == adAuth.IsAuthenticated(txtDomain.Text, txtUsername.Text, txtPassword.Text))
    {
      //string groups = adAuth.GetGroups();
      string groups = txtUsername.Text;
      //Create the ticket, and add the groups.
      bool isCookiePersistent = chkPersist.Checked;
      FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, 
                txtUsername.Text,DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups);

      //Encrypt the ticket.
      string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

      //Create a cookie, and then add the encrypted ticket to the cookie as data.
      HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

      if(true == isCookiePersistent)
      authCookie.Expires = authTicket.Expiration;

      //Add the cookie to the outgoing cookies collection.
      Response.Cookies.Add(authCookie);

      //You can redirect now.
      Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, false));
    }
    else
    {
      errorLabel.Text = "Authentication did not succeed. Check user name and password.";
    }
  }
  catch(Exception ex)
  {
    errorLabel.Text = "Error authenticating. " + ex.Message;
  }
}
</script>


in that code i have domain, username and password TextBox. And they will be authenticated in ldap.

here's my ldapauthentication.cs code:

using System;
using System.Text;
using System.Collections;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.ActiveDirectory;
 
namespace FormsAuth
{
    public class LdapAuthentication
    {
        private string _path;
        private string _filterAttribute;
        
        public LdapAuthentication(string path)
        {
            _path = path;
        }
 
        public bool IsAuthenticated(string domain, string username, string pwd)
        {
            string _domain = domain;
            string domainAndUsername = domain + @"\" + username;
            DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
 
            try
            {
                //Bind to the native AdsObject to force authentication.
                object obj = entry.NativeObject;
 
                DirectorySearcher search = new DirectorySearcher(entry);
 
                search.Filter = "(SAMAccountName=" + username + ")";
                search.PropertiesToLoad.Add("cn");
                SearchResult result = search.FindOne();
 
                if (null == result)
                {
                    return false;
                }
 
                //Update the new path to the user in the directory.
                _path = result.Path;
                _filterAttribute = (string)result.Properties["cn"][0];
            }
            catch (Exception ex)
            {
                throw new Exception("Error authenticating user. " + ex.Message);
            }
 
            return true;
        }
 
        public string GetGroups()
        {
            DirectorySearcher search = new DirectorySearcher(_path);
            search.Filter = "(cn=" + _filterAttribute + ")";
            search.PropertiesToLoad.Add("memberOf");
            StringBuilder groupNames = new StringBuilder();
 
            try
            {
                SearchResult result = search.FindOne();
                int propertyCount = result.Properties["memberOf"].Count;
                string dn;
                int equalsIndex, commaIndex;
 
                for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
                {
                    dn = (string)result.Properties["memberOf"][propertyCounter];
                    equalsIndex = dn.IndexOf("=", 1);
                    commaIndex = dn.IndexOf(",", 1);
                    if (-1 == equalsIndex)
                    {
                        return null;
                    }
                    groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
                    groupNames.Append("|");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error obtaining group names. " + ex.Message);
            }
            return groupNames.ToString();
        }
 
        
    }
}


how can i use the value of logon form in to change password form?
i want to change password.

What I have tried:

I tried to make new parameters, but i still confuse.
I just want to take value of old password and change it to new password.
can somebody help me?

解决方案

You could store the old password in Session. Other option is to use Profile Provider or perhaps creating a custom class that holds the information you need after logon. You can also look into ASP.NET Identity to add custom claims. But I would prefer to let the user type their old password for changing new password for security reasons.


i suggest you that you must ask old password to change password
other wise if you just want to change it with logon so you need to use Section
basically section is pass data from one form to other and you can call session any where

To add value in session variable

Session.Add("Programe",  ddlProgamme.SelectedValue);


"Program" is a name of session variable
it's data type is by default string

To Get value from session variable

this.program = (string)(Session["Programe"]);


这篇关于如何在另一个页面ASP.NET中使用文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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