locked
How can I force users to enter a username and password (redirected from Visual Studio discussion) RRS feed

  • Question

  • When I originally set up my project I 'hard-coded' my username and password in so that I could access a remote DB2 database. Now that I need to publish it to other users they need to be prompted for their own usernames and passwords. I've gone to "My Project/Settings" and deleted the items from the string - but when I run I get the following message on the statement:

    Me.BATCHTableAdapter.Fill(Me.DB1DataSet.BATCH)

    ERROR [08001] [IBM] SQL30082N  Security processing failed with reason "3" ("PASSWORD MISSING").  SQLSTATE=08001

    The settings are as follows (before I delete the userid and password):

    Name:DB1ConnectionString
    Type:(Connection string)
    Scope:Application
    Value:Database=DB1;Server=99.999.999.99:446;userid=myuser;password=mypassword;Persist Security Info=True;

    The question is what do I have to do to make it forget my userid and password - and always prompt the user for both when running up?

    Installed Visual Studio 2005 SP2 (with Visual Basic 2005)

    (this was asked in the Visual Studio conference  - they redirected me here)

    Thanks in advance - Steve

    Thursday, December 9, 2010 4:02 PM

Answers

  • I think what you are asking for is how to remove those hardcoded values and replace it with values from textbox?

    You can build a connection string like this and use it to open connection, or pass to command object etc.

    connectionString = String.Format("Database=DB1;Server=99.999.999.99:446;userid={0};password={1};Persist Security Info=True;", UserIdTextBox.Text, PasswordTextBox.Text)


    Pradeep, Microsoft MVP (Visual Basic)
    • Proposed as answer by WickedTackleBox Tuesday, December 14, 2010 10:39 AM
    • Marked as answer by Kee Poppy Friday, December 17, 2010 1:56 AM
    Tuesday, December 14, 2010 10:29 AM

All replies

  • Hi,

     Forgive if I'm wrong, due to the fact I am still learning but, at some point in your code did you have to set the Value of "myuser" and "mypassword" ? Could you do something like the following...

     Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccept.Click
        Dim myuser As String
        Dim mypassword As String
    
        'sanity check
        If tbUser.Text = "" Then
          MsgBox("Please Provide User Name", MsgBoxStyle.Exclamation)
         Exit Sub
        End If
        If tbPassword.Text = "" Then 
          MsgBox("Please Provide Password", MsgBoxStyle.Exclamation)
          Exit Sub
        End If
    
        myuser    = tbUser.Text
        mypassword = tbPassword.Text
     
        Try
           YourProcess(myuser, mypassword)
        Catch ex As Exception
           MsgBox("ERROR: " & ex.Message, MsgBoxStyle.Critical)
           Exit Sub
        End Try
     End Sub
    
    
    • Proposed as answer by Cor Ligthert Friday, December 10, 2010 6:31 AM
    Thursday, December 9, 2010 5:35 PM
  • Hi,

     Upon further contemplation I realize that well, it's a bit more complicated, the above code would make the user input name and password but it wouldn't seem to matter what they put in, thus rendering the user name and password pointless. If you plan to depploy your application within your own company and you have an idea of who is going to need to access your database mabee you can have a preset list in a dill file or a database for holding login info.

     Like I stated in my previous post, I'm still learning and still "new" so I am posting not onlly for the purpose of trying to help you whether directly or inadvertently but also I find the interaction with others to be very inlighting.

    • Edited by WickedTackleBox Saturday, December 11, 2010 3:52 AM Spelling correction
    Friday, December 10, 2010 2:31 AM
  • Steve,

    Somehow you need to give the UserName and the Password to this IBM DB2 password, you can use the sample WickedTacleBox for you.

    You then have to put the username and password then in the Connectionstring for that IBM DB2 database and set a try and catch around the connection

    Something like this in pseudo

    Using Con as new connection(TheConnectionstringWithThePasswordAndUserNamePastedIn)
        Try
           Con.Open
           Try
           using Whatever other Ado
           Catch ex as Exception
                the  Whatever other Ado
            End Try
         Catch
            Messagebox.Show("Probably your userId and Password are wrong"
         End Try
    End Using

     


        


    Success
    Cor
    Friday, December 10, 2010 6:31 AM
  • Hi Steve,

    Have you checked WichedTackleBox and Cor's reply? Were they helpful?

    Basically, do not put user id and password these security things in your app.config, instead let the other users input them in textboxes or dialog, which was mentioned by WichedTackleBox and Cor. With doing that, we can get the new connection string.

     

    Best Regards,

    Kee

     


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 14, 2010 9:11 AM
  • I think what you are asking for is how to remove those hardcoded values and replace it with values from textbox?

    You can build a connection string like this and use it to open connection, or pass to command object etc.

    connectionString = String.Format("Database=DB1;Server=99.999.999.99:446;userid={0};password={1};Persist Security Info=True;", UserIdTextBox.Text, PasswordTextBox.Text)


    Pradeep, Microsoft MVP (Visual Basic)
    • Proposed as answer by WickedTackleBox Tuesday, December 14, 2010 10:39 AM
    • Marked as answer by Kee Poppy Friday, December 17, 2010 1:56 AM
    Tuesday, December 14, 2010 10:29 AM
  • Hi there, thanks to you all for trying to help me. I understand that I need to replace the hard-coded stuff
    with the result of an input box or similar.

    In the whole of my project I can find the following occurances of the use of the hard-coded passwords (generated by the drag and drop creation of the original connection string):

    1) In Settings.Designer.vb I have the following lines

             Global.System.Configuration.DefaultSettingValueAttribute("Database=DBNAME;User ID=myuser;Password=mypassword;Server=xx.xxx.xxx.x:xxxx"& _
                "0;Persist Security Info=True")>  _
            Public ReadOnly Property DBNAMEConnectionString() As String
                Get
                    Return CType(Me("DBNAMEConnectionString"),String)
                End Get
            End Property


    2) In app.config the line

        <connectionStrings>
            <add name="PROJ.My.MySettings.DBNAMEConnectionString" connectionString="Database=DBNAME;User ID=myuser;Password=mypassword;Server=xx.xxx.xxx.x:xxxxx;Persist Security Info=True"
                providerName="IBM.Data.DB2" />
        </connectionStrings>

     

    3) In Settings.settings the lines

        <Setting Name="DBNAMEConnectionString" Type="(Connection string)" Scope="Application">
          <DesignTimeValue Profile="(Default)">&lt;?xml version="1.0" encoding="utf-16"?&gt;
    &lt;SerializableConnectionString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;ConnectionString&gt;Database=DBNAME;User ID=myuser;Password=mypassword;Server=xx.xxx.xxx.x:xxxxx;Persist Security Info=True&lt;/ConnectionString&gt;
      &lt;ProviderName&gt;IBM.Data.DB2&lt;/ProviderName&gt;
    &lt;/SerializableConnectionString&gt;</DesignTimeValue>
          <Value Profile="(Default)">Database=DBNAME;User ID=myuser;Password=mypassword;Server=xx.xxx.xxx.x:xxxxx;Persist Security Info=True</Value>
        </Setting>


    I suppose my problem is which (any or all) of the above lines do I need to change - and what do I need to change them to?

    I wouldn't be having this problem if my boss had sent me on the proper courses :-)

    Cheers,

    Steve


    s r campbell
    Tuesday, January 4, 2011 3:35 PM
  • Do you want to ask the user to enter those values every time, or do you want those values to be automatically picked from settings file?

    In case you want the user to input those values (username & password) then you would have to show them two textboxes and then use code similar to what I posted earlier.

    In case you want that to be picked up automatically from the settings file, just replace those values in the file. Everything is probably already setup just as you need.


    Pradeep, Microsoft MVP (Visual Basic)
    Thursday, January 6, 2011 9:08 AM
  • Well, we're nearly there Pradeep.  I have setup a login form as the startup form to get the username and password. Those values are then being passed to the subsequent form ok - but I'm getting an "Invalid Argument" error as below. I think it's almost there, it probably just needs a tweak to the connection string value.

    In the Settings section for "My Project" I have the Name as DBname, Type as Connection String, Scope as Application and Value set to

    String.Format("Database=DBname;User ID={0};Password={1};Server=xx.xxx.xxx.x:xxxxx;Persist Security Info=True;",MyLoginForm.FormUsername.Text, MyLoginForm.FormPassword.Text)

    (I've tried all sorts of variations on the Value, with and without quotes and brackets etc.)

    When I run it I get the login form ok - enter the ID and password - I display these when entering the main form ok - then I get the following error box when it tries to access the DB:

    ! Argument Exception was unhandled

    Invalid Argument

    This poiints to the following section of code:

    <

    Global.System.Diagnostics.DebuggerNonUserCodeAttribute()> _

     

    Friend Property Connection() As Global.IBM.Data.DB2.DB2Connection

     

    Get

     

    If (Me._connection Is Nothing) Then

     

    Me.InitConnection

     

    End If

     

    Return Me._connection

     

    End Get

    Cheers.

    Steve


    s r campbell
    Thursday, January 6, 2011 1:10 PM
  • In the Settings section for "My Project" I have the Name as DBname, Type as Connection String, Scope as Application and Value set to

    String.Format("Database=DBname;User ID={0};Password={1};Server=xx.xxx.xxx.x:xxxxx;Persist Security Info=True;",MyLoginForm.FormUsername.Text, MyLoginForm.FormPassword.Text)

    (I've tried all sorts of variations on the Value, with and without quotes and brackets etc.)


    You should decide wheter to use the settings file or ask user to fill it every time. From what you have described above, it looks like you want the user to fill in the username and password each time. So you don't change anything in the settings file. (you can remove them later if you want to because those values will be redundent)

    You will use the values provided by the user and add whatever additional values you want to to it (username and password)

    so something like this:

    Dim connectionString as string

    connectionString = String.Format("Database=DBname;User ID={0};Password={1};Server=xx.xxx.xxx.x:xxxxx;Persist Security Info=True;",MyLoginForm.FormUsername.Text, MyLoginForm.FormPassword.Text)

     

    Then use this connectionString variable instead of your Connection property.

     


    Pradeep, Microsoft MVP (Visual Basic)
    Thursday, January 6, 2011 8:44 PM
  • Nearly there - just a compilation error to get rid of now. The app kept uasing the Connection Property so I deleted it and added the string as you suggested in the app itself.

    The compilation error is "DBNAMEConnectionString is not a member of MYPROJ.My.MySettings" and points to a small section of code in DBNAMEDataset.Designer.vb

    <

    Global.System.Diagnostics.DebuggerNonUserCodeAttribute()> _

     

    Private Sub InitConnection()

     

    Me._connection = New Global.IBM.Data.DB2.DB2Connection

     

    Me._connection.ConnectionString = Global.MYPROJ.My.MySettings.Default.DBNAMEConnectionString

     

    End Sub

    Hopefully it might work if I can find how to get round this compilation error.

    Cheers,

    Steve


    s r campbell
    Friday, January 7, 2011 1:17 PM
  • Please avoid editing the settings file directly until you are experienced enough to understand what will be affected where.

    The best way is always to let visual studio do it. So you should put back those lines and instead use the interface provided to do the job.

    In the Solution Explorer window, right click your project and click Properties from popup menu. Go to the settings tab and remove it from there.

     


    Pradeep, Microsoft MVP (Visual Basic)
    Friday, January 7, 2011 1:46 PM
  • Hi Pradeep,

    When I said I deleted it that's exactly what I did (used the interface as you described), and immediately the error appears in the listbox below.

    Cheers,

    Steve


    s r campbell
    Friday, January 7, 2011 1:58 PM
  • What happens if you delete that property? Was that connection being used somewhere (some control at design time etc.)?


    Pradeep, Microsoft MVP (Visual Basic)
    Friday, January 7, 2011 7:34 PM
  • Good question Pradeep,

    As I see it the scenarios are as follows:

    1)   If I delete the connetion property from "My Project"/"Settings" I immediately get the message
    'ConnectionString' is not a member of 'MYPROJ.My.MySettings'

    2)   If I leave the connection property alone it will be used in preference to the connection string I define.

    I think that I have probably defined the connection string in the wrong module.

    At the moment it's defined when loading the form, just before the first database access like this...

    Public Class MYform
            dim ConnectionString as String

    Private Sub Form1_Load( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles MyBase.Load
            ConnectionString=String.Format("Database=DBNAME;User ID={0};Password={1};Server=xx.xxx.xxx.x:xxxxx;Persist Security Info=True;",
     MyUsername.Text, MyPassword.Text)

            Me.MYTABLETableAdapter.Fill(Me.DBNAMEDataSet.MYTABLE)

    I suppose the question is should the definitions be in a different or more appropriate module?

    Cheers,

    Steve


    s r campbell
    Monday, January 10, 2011 11:30 AM
  • If you put that intialization in Form_Load event, it won't work because the textboxes are empty at that time. I think the correct time to initialize it is on the click of your Login button etc.

     


    Pradeep, Microsoft MVP (Visual Basic)
    Monday, January 10, 2011 12:17 PM
  • I have a "Login Form" which runs before my application form.

    The user and password are entered ok - so now I have the connection string defined :

    Public Class MyLoginForm

            dim ConnectionString as String

        Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
     
            ConnectionString=String.Format("Database=DBNAME;User ID={0};Password={1};Server=xx.xxx.xxx.x:xxxxx;Persist Security Info=True;",
      Me.MyUsername.Text, Me.MyPassword.Text)

            my_main_app.Show

            Me.Close()

        End Sub

    The problem appears to be that "ConnectionString" is not declared according to the application form

    In your last reply you said it should go in the click of the Login Button - I'm not sure which
    Login Button you mean if its not the OK_Click of the MyLoginForm button.

    If you mean a different place then please let me know. :-)

    Cheers,

    Steve


    s r campbell
    Monday, January 10, 2011 1:44 PM
  • Yes, that's OK. In your previous post you were doing in Form_Load which is not the appropriate place.

    I think now the problem is that the ConnectionString declaration is local to your MyLoginForm. So as soon as it is closed, the value is lost (unless you did something to persist it, not shown in your code here).

    If you want this variable to be available throughout the application to all forms/classes, you should declare it in a Module.

    e.g.

     

    Module MyGlobals
    
      Public ConnectionString As String
    
    End Module
    

     

     

    So now in your MyLoginForm form you can set it, and in your other forms you can access its values.


    Pradeep, Microsoft MVP (Visual Basic)
    Tuesday, January 11, 2011 2:17 PM
  • I can't believe how close we are to solving this!

    I've set up the global string:

     Module MyGlobals
          Public ConnectionString As String
     End Module

    Run the login form ok, then check the contents of connection string in the main
    application form:

    Private Sub Form1_Load( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles MyBase.Load

           msgbox ("ConnectionString=*" & ConnectionString)

            Me.MYTABLETableAdapter.Fill(Me.DBNAMEDataSet.MYTABLE) ' failed line

    ...

    End Sub

    Everything in the string is as expected, username, password, IP address of the database etc.

    It then falls over on the "failed line" with "Invalid password" as it's still trying to use the connection property
    which has an invalid password in it (so I know whether or not it's being used).

    If I delete the connection property an error appears immediately in the Error list :

    'DBNAMEConnectionString is not a member of MYPROJ.MY.MySettings"

    So I renamed the ConnectionString to DBNAMEConnectionString but it doesn't clear the errors which are in:

    DBNAMEDataset.Designer.vb

          
            <Global.System.Diagnostics.DebuggerNonUserCodeAttribute()>  _
            Private Sub InitConnection()
                Me._connection = New Global.IBM.Data.DB2.DB2Connection
                Me._connection.ConnectionString = Global.MYPROJ.My.MySettings.Default.DBNAMEConnectionString
            End Sub

    I think we're almost ther! Thanks for your input and patience.

    Steve

    • Edited by SteveCam Wednesday, January 12, 2011 1:45 PM formatting
    Wednesday, January 12, 2011 1:44 PM
  • I'm getting an inking that you have used the designtime components for dataset & dataadapter. You should choose one way... either create & use things at runtime or use the designtime controls. Mixing them up will always create confusion even if you somehow reslove it now.

     

    Try this:

     

     

    ConnectionString = String.Format("Database=DBNAME;User ID={0};Password={1};Server=xx.xxx.xxx.x:xxxxx;Persist Security Info=True;",
     Me.MyUsername.Text, Me.MyPassword.Text)
    
    Global.MYPROJ.My.MySettings.Default.DBNAMEConnectionString = ConnectionString
    
    my_main_app.Show
    

     


    Pradeep, Microsoft MVP (Visual Basic)
    Wednesday, January 19, 2011 12:15 PM
  • Hi again,

     

    I added the line

    Global.MYPROJ.My.MySettings.Default.DBNAMEConnectionString = ConnectionString

    and now have the error Property 'DBNAMEConnectionString' is ReadOnly.

    In your previous reply, were you trying to say that I shouldn't be using the dataset & adapter if I want the user to input their credentials - instead I should remove them and code it all?

    Cheers,

    Steve

     


    s r campbell
    Thursday, January 20, 2011 2:54 PM
  • You would be using the dataset and dataadapter anyways.

    What I was saying is not to use the design time component from the toolbox. Instead create them via code. Its hardly a few lines of code.

     


    Pradeep, Microsoft MVP (Visual Basic)
    Thursday, January 20, 2011 3:13 PM
  • Ok - I'll have a go at that.

    In the meantime - how do I fix the "read " problem with the connection string?

    Cheers

    Steve


    s r campbell
    Thursday, January 20, 2011 4:12 PM