使用app.config中的连接字符串运行时用户和PW [英] Runtime User and PW using connection string from app.config

查看:65
本文介绍了使用app.config中的连接字符串运行时用户和PW的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想创建一个WPF应用程序。

目前我有登录表,用户可以输入他的名字和密码(文本框)对于每个)并按连接按钮。



我打算做的是在App.Config文件中编写连接字符串(不带用户名和密码),然后从中读取用户名和密码两个文本框,以便连接到数据库。

要验证凭据并继续下一个表单,将从名为Users的表中进行选择查询,以比较输入的文本。



到目前为止,我设法编写以下代码:

- App.config文件:

 < ;   xml     version   =  1.0   编码  =  utf-8  >  
< 配置 >
< connectionStri ngs >
< add name = MyDatabase providerName = Npgsql connectionString = Server = localhost ;端口= 1234; Database = MyDatabase; / >
< / connectionStrings >
< / configuration >





- Login.xaml.cs:

  private   void  btnLogin_Click( object  sender,RoutedEventArgs e)
{
string PGstring = ConfigurationManager.ConnectionStrings [ MyDatabase].ConnectionString;
var PGcomm = new NpgsqlCommand();
...?
...?
}





我有记住一些想法:

1.在app.config中使用appSettings中的键值,然后传递字符串user = tbUser.txt;

2.使用StringBuilder完成带有Append()的app.config中的字符串;



但这些可能不是一个好方法,这就是我想请求你帮助的原因。
如果可以,请提供一些建议或示例。



非常感谢!

解决方案

< blockquote>使用基于主机的身份验证。



http://www.postgresql.org/docs/9.4/static/auth-pg-hba-conf.html [ ^ ]



将用户名和密码传递给postgresql。让它管理谁有权访问什么。在您的应用程序中存储数据库凭据是一个坏主意。


我试图将修改app.config:

 <  配置 >  
< span class =code-keyword>< connectionStrings >
< add 名称 = MyDatabase connectionString = Server = localhost ; Port = 1234; Database = MyDatabase; User Id = myuser; Password = mypw; / >
< / connectionStrings >
< appSettings >
< add key = myuser / >
< add key = mypw / >
< / appSettings >
< / configuration >



并且在按钮事件中:

  string  myuser = ConfigurationManager.AppSettings [  myuser ]的ToString(); 
string mypw = ConfigurationManager.AppSettings [ 。mypw]的ToString();
string PGstring = ConfigurationManager.ConnectionStrings [ MyDatabase]。ConnectionString;



然后传递

 myuser = tbUser.Text; 
mypw = tbPw.Text;



但是当试图测试它时会显示以下消息:

密码验证失败对于用户myuser...

无论我在文本框中输入什么内容。


Hello,
I am looking to create a WPF Application.
At the moment I have the Login Form, where user can enter his name and password(a textbox for each) and press Connect button.

What I intend to do, is to write the connection string (without Username and Password) in the App.Config file, and then to read the Username and Password from the two textboxes, in order to connect to the database.
To verify the credentials and proceed to next Form, will make a select query from a table called Users to compare the text entered.

So far I managed to code the following:
- App.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="MyDatabase" providerName="Npgsql" connectionString ="Server=localhost; Port=1234; Database=MyDatabase;"/>
  </connectionStrings>
</configuration>



- Login.xaml.cs:

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
   string PGstring = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
   var PGcomm = new NpgsqlCommand();
   ...?
   ...?
}



I have in mind some ideas:
1.Use key,value in appSettings in app.config and then pass string user = tbUser.txt;
2.Use StringBuilder to complete the string in app.config with Append();

But these may not be a good approach and that's why I would like to ask for your help.
If you can please give some advices or examples.

Thank you very much!

解决方案

Use host based authentication.

http://www.postgresql.org/docs/9.4/static/auth-pg-hba-conf.html[^]

Pass the user name and password to postgresql. Let it manage who has access to what. Storing DB credentials inside your application is a bad idea.


I have tried to put modify app.config:

<configuration>
  <connectionStrings>
    <add name="MyDatabase" connectionString="Server=localhost; Port=1234; Database=MyDatabase; User Id=myuser; Password=mypw;"/>
  </connectionStrings>
  <appSettings>
    <add key="myuser"/>
    <add key="mypw"/>
  </appSettings>
</configuration>


And in button event:

string myuser = ConfigurationManager.AppSettings["myuser"].ToString();
string mypw = ConfigurationManager.AppSettings["mypw"].ToString();
string PGstring = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;


and then pass

myuser = tbUser.Text;
mypw = tbPw.Text;


But when try to test it displays the message:
"password authentication failed for user "myuser" "...
no matter what I type in textbox.


这篇关于使用app.config中的连接字符串运行时用户和PW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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