我有一个带登录界面的程序。用户可以通过选择单选按钮选择通过Windows身份验证模式或SQL服务器身份验证模式登录。以下是界面的快照:
[1]: https://i.stack.imgur.com/TWKQO.png
当用户选择Windows身份验证时,用户无需在用户名和密码文本框中输入任何凭据。如果用户选择SQL server auth,则用户必须填写文本框上的凭据。因此,我想将windows auth的文本框设置为空白或不可见,这样如果用户尝试使用,则不允许用户输入凭据。
以下是代码:
private void btn_Login_Click(object sender, EventArgs e)
{
bool useWindowsAuth = WindowsAuth.Checked; // Check radio button for windows authentication
string userName = string.Empty;
string password = string.Empty;
// Windows authentication mode : if textbox are empty , display message
if (!useWindowsAuth)
{
userName = textBox1.Text;
password = textBox2.Text;
if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password))
{
MessageBox.Show("Please provide Username and Password");
return;
}
}
//create sql connection to both authentication mode
var connStrBldr = new System.Data.SqlClient.SqlConnectionStringBuilder();
connStrBldr.DataSource = "XXXXXXXXXX";
connStrBldr.InitialCatalog = "Test";
if (useWindowsAuth)
{
connStrBldr.IntegratedSecurity = true; // accepts user login without any credentials
}
else
{
connStrBldr.IntegratedSecurity = false; // accepts user login with credential stored in sql server
connStrBldr.UserID = userName;
connStrBldr.Password = password;
}
bool validUser = true;
try
{
using (SqlConnection con = new SqlConnection(connStrBldr.ToString()))
{
con.Open();
//lookup on login credentials in sql server
}
}
catch (SqlException) // An exception will be caught if invalid credentials were used.
{
validUser = false;
}
//validate if user is successful or failed
if (validUser)
{
MessageBox.Show("Login successful!");
this.Hide();
MainPage main = new MainPage();
main.Show();
}
else
{
MessageBox.Show("Login failed!");
}
}
我需要什么属性来实现它?
答案 0 :(得分:2)
这就是你要找的东西吗?
textBox1.Enabled = false;