Monday, December 20, 2010

How To Build a 2-Tier ASP.NET 4.0 Web Site–Part 3

Now that IIS 7.5 is installed on Windows 7 let’s move on to deploying the website to IIS and testing it there.
  1. From the Build menu choose Publish Web Site:
    2010-12-01_232940
  2. You can either accept the default location or change it to something else:
    2010-12-01_232958
    Make note of the location chosen as you will need it shortly.
  3. Now let’s configure the website in IIS 7. Launch IIS manager from Control Panel | Administrative Tools | Internet Information Services (IIS) Manager:
    2010-12-01_225342
    2010-12-01_225416
    Hint: IIS Manager is the configuration tool for IIS and you’ll be jumping into it often. You can use the Run dialog for faster access via the Windows + R then typing “inetmgr”:

    2010-12-01_225504
  4. Expand the tree control on the left, right-click on Default Web Site then right click and choose Add Application:
    2010-12-01_233031
  5. Type in an Alias name that will be used as the website name when browsing, Select the ASP.NET v4.0 Application pool and choose the same Physical path where you just published the website to previously. You should click the Test Settings button to verify that Authentication and Authorization are configured correctly:
    2010-12-01_233205
    2010-12-01_233703
  6. If you received the above Authorization error it means that IIS does not have permission to access the folder where you published the website to. If this occurs, click Close then OK to complete the creation of the application. Back in the tree view control right-click on Edit Permissions to grant the IIS web server permission to the publish folder:
    2010-12-01_233315
  7. On the Security tab choose Edit, then Add, then Advanced, and finally Find Now:
    2010-12-01_2333482010-12-01_2334042010-12-01_2334232010-12-01_233445
  8. From the list of users and groups use Control + click to select the follow three:
    • IIS_IUSRS
    • IUSR
    • NETWORK SERVICE
    2010-12-01_233512 Now you can click OK through until the dialogs are dismissed, right-click on the Website in the tree control again and choose Edit Application where you can Test Settings again.
  9. When testing from within Visual Studio we are running under the permissions of ourselves – the account we signed into Windows with. When browsing against IIS, it is IIS that is connecting to the database. Since the IIS web server will be acting as a proxy for the user when accessing the database, you must grant the IIS user permission to the database. Using SQL Server Management Studio, execute the following T-SQL statements:
    CREATE LOGIN [IIS APPPOOL\ASP.NET v4.0] 
      FROM WINDOWS 
      WITH DEFAULT_DATABASE=[master], 
      DEFAULT_LANGUAGE=[us_english]
    GO
    
    CREATE USER [AdventureWorksUser] 
      FOR LOGIN [IIS APPPOOL\ASP.NET v4.0]
    GO
    
    EXEC sp_addrolemember 'db_datareader', 'AdventureWorksUser'
    GO
    

    2010-12-02_000021
  10. Finally right-click on the website again, choose Browse:
    2010-12-01_233228
  11. Sit back and enjoy the fruits of your hard labor:
    2010-12-02_000036
While testing the site in IIS is not absolutely required, it is helpful to go through the publishing process to help shake out potential problems such as the fact that IIS runs under a service account and not the you (the logged in user) the way Visual Studio’s build in web server does.

No comments:

Post a Comment