{"id":1732,"date":"2008-11-22T05:59:51","date_gmt":"2008-11-22T09:59:51","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=1732"},"modified":"2008-11-22T05:59:51","modified_gmt":"2008-11-22T09:59:51","slug":"configuring-authentication-on-ms-sql-2008","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/database-tutorials\/configuring-authentication-on-ms-sql-2008","title":{"rendered":"Configuring Authentication on MS SQL 2008"},"content":{"rendered":"
This tutorial will walk you through configuring you server with credentials for applications and users to connect with. This will allow you to determine who has what type of access as well as how they can update the tables in the database. <\/p>\n
\tFirst you need to decide which method you are going to use to give users access to the database. The first (and slightly less secure way) is to create an actual login for each person going to use the database. This will require you to get the users credentials in a form before attempting a connection to the database. The alternative to this method is to create only on login for a specific application and then create a table with an encrypted field for the password for the users. Then when the application connects to the database (using the credentials you created every time), it will look into a table to see if the credentials the user supplied are correct. I will walk you though both methods in this tutorial. Both steps require you to start from the beginning. Let\u2019s get started.<\/p>\n
First let\u2019s open our Microsoft SQL Server Management Studio by going to Start->Programs->Microsoft SQL Server 2008->Microsoft SQL Server Management Studio.<\/p>\n
<\/a><\/p>\n Once in, you should see a pane to the left of the window that looks like the above image. Expand the Security group.<\/p>\n <\/a><\/p>\n You screen should now look like above.<\/p>\n <\/a><\/p>\n Right-click on the logins group and select new login. <\/p>\n <\/a><\/p>\n You will then be presented the above screen. This is the screen we will use to configure the user to have access to the database.<\/p>\n <\/a><\/p>\n Fill in the login name with what you want the username to be. If you are using the table method for authenticating, it is common practice to name this something like \u201cappname_login\u201d where appname is the name of your application that is connecting to it. Select the SQL Server Authentication radial button. Now you need to type in the password you want that user to start out with in the boxes provided twice. Then select whether they will be forced to adhere to password policies (change x amount of days, certain length, certain types of text, ect\u2026), and password expirations. Also check the box if you want the user to have to change there password the next time they log-on. Finally select the default database (which in this case is TT) and select the default language. <\/p>\n <\/a><\/p>\n Now, in the left pane, click on User Mapping and check the box for the database we want the user to have access to and then select the access type from the bottom window on the right that you want them to have.<\/p>\n <\/a><\/p>\n Now if you expand the Login directory you will see the user we just created. The user will also appear listed under the users group that is under the security group of TT since we mapped his credentials to that database. Simply do this for each user that you want to have access to the database if you are using the login for each user method. If you do not want to use the table method, you can discontinue reading here as the remaining steps will be fore creating a table for credentials and how to set it up. There are two ways to create a table, you can do it in the Graphical Interface, or you can write the script yourself. What I would recommend if you are new to SQL is to create it in the graphical interface and then view the code that it creates so that you can get an understanding of what the commands and syntax of SQL are. Let\u2019s do it in the graphical interface:<\/p>\n <\/a><\/p>\n Expand the databases collection, and then expand your database that you created. Once you do that right click on the table collections and select New Table\u2026<\/p>\n <\/a><\/p>\n Once the information loads, you will see a properties window to the right that will allow you to name the table. Name it something like \u2018Users\u2019 or \u2018Credentials\u2019. Once you do that we need to fill out the information for the table.<\/p>\n <\/a><\/p>\n To create the info just begin typing in the appropriate box. Once you insert information into the box and move to the next field, the builder will auto-create the next line(column for the table). Make three columns to match what I have above. This will allow us to store the username, password, as role as a role for the user. This will allow us to use this field in our application to determine what the users can and can\u2019t do easier. Also note that I have made the length of the password 255 characters. This is because I intend to encrypt the password that goes into this field which will take up more characters than required. I will cover this in a future tutorial.<\/p>\n <\/a><\/p>\n Now that we have finished making our changes we can save the table but first let\u2019s take a look at the code that this is going to generate. Right-click anywhere on the editor window and a menu will pop-up allowing you to select \u2018Generate Change Script\u2026\u2019. <\/p>\n <\/a><\/p>\n Once you select that menu option, the above window will appear with the script that the system would run to create the table. Look over it and try to understand what they do. Look the commands up in the help file and Google them to see what they do. This will be a good way to understand the commands. You can copy and paste this information into an SQL file and run it to create the table. Click on No to close the window once you are done and you will be taken back to the original screen. <\/p>\n <\/a><\/p>\n Once you have everything in the table we will need, we can right-click on the tab for the editor and select save. This will save the table to our database.<\/p>\n <\/a><\/p>\n Once the file finishes saving you should see it in the tables folder if you expand it\u2019s view as above.<\/p>\n <\/a><\/p>\n Now we need to insert some info into the table. Right-click on our database and select New Query as pictured above.<\/p>\n The query to insert people in this table will be as follows: -Note- It is good practice to capitalize all SQL command and tables so that you get differentiate the text easily.<\/p>\n For example, if I wanted to create a user in the table for me with the password of password123 and the role of Admin the query would look like this:<\/p>\n Type your query into the text box and click on the green play button on the top portion of the screen to run the query.<\/p>\n\r\nINSERT INTO DBName..TableName VALUES(\u2018UserName\u2019,\u2019Password\u2019,\u2019Role\u2019)\r\n<\/pre>\n
\r\nINSERT INTO TT..USERS VALUES('MMaguire','password123','Admin')\r\n<\/pre>\n