03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

804 x APPENDIX B CONFIGURING SQL SERVER 2012<br />

5. If you want to set f<strong>in</strong>e-gra<strong>in</strong>ed security options for your database objects, click the Securables<br />

option visible <strong>in</strong> Figure B-6. This dialog box enables you to determ<strong>in</strong>e permissions for the<br />

user account on objects <strong>in</strong> your database like tables, views, <strong>and</strong> stored procedures. For the<br />

Planet Wrox website, you don’t need to make any changes <strong>in</strong> this dialog box.<br />

6. F<strong>in</strong>ally, click OK to create the database user <strong>and</strong> assign it to the db_datareader <strong>and</strong> db_<br />

datawriter roles.<br />

Now that SQL Server <strong>and</strong> your user accounts are configured correctly, the f<strong>in</strong>al step is to configure<br />

the website to use this new user account.<br />

1. Open the Web.config file of the Planet Wrox application from the C:\Beg<strong>ASP</strong><strong>NET</strong>\Release<br />

folder.<br />

2. Modify the element so it ends up like this:<br />

<br />

<br />

<br />

<br />

In your configuration file, each connection str<strong>in</strong>g should be on a s<strong>in</strong>gle l<strong>in</strong>e. Don’t forget<br />

to replace the value ServerName <strong>in</strong> the Data Source attributes with a valid server name.<br />

Depend<strong>in</strong>g on your server <strong>and</strong> configuration, this could be as simple as (local) or .\<br />

SqlExpress to po<strong>in</strong>t to a SQL Server on the local mach<strong>in</strong>e, DatabaseServer to po<strong>in</strong>t to a<br />

server on the network called DatabaseServer, or someth<strong>in</strong>g like DatabaseServer\Sql2012<br />

that po<strong>in</strong>ts to a named <strong>in</strong>stance called Sql2012 on a mach<strong>in</strong>e called DatabaseServer.<br />

3. Save the changes <strong>and</strong> then open the site by start<strong>in</strong>g your browser <strong>and</strong> go<strong>in</strong>g to http://<br />

localhost. Everyth<strong>in</strong>g should still work as expected, but the site now no longer uses the<br />

databases <strong>in</strong> the App_Data folder; it uses the SQL Server def<strong>in</strong>ed <strong>in</strong> your connection str<strong>in</strong>gs<br />

<strong>in</strong>stead through W<strong>in</strong>dows Authentication, as identified by the Trusted_Connection=True<br />

attribute <strong>in</strong> the connection str<strong>in</strong>g.<br />

If you get an error when brows<strong>in</strong>g to the site, you may need to turn off custom errors <strong>in</strong> the<br />

Web.config file (or set it to RemoteOnly) to see the actual error message. Possible reasons<br />

for the error are an <strong>in</strong>correct server name <strong>in</strong> the connection str<strong>in</strong>g <strong>and</strong> an <strong>in</strong>correctly configured<br />

database role membership for the configured account.<br />

Once you f<strong>in</strong>d out the correct account <strong>and</strong> have configured SQL Server correctly, us<strong>in</strong>g W<strong>in</strong>dows<br />

Authentication isn’t that hard. In fact, your connection str<strong>in</strong>g now becomes a little easier <strong>and</strong> more<br />

secure, because you don’t need to store a username <strong>and</strong> password <strong>in</strong> it anymore.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!