"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Authenticating Users with Forms Authentication (C#)

by Microsoft

Learn how to use the [Authorize] attribute to password protect particular pages in your MVC application. You learn how to use the Web Site Administration Tool to create and manage users and roles. You also learn how to configure where user account and role information is stored.

The goal of this tutorial is to explain how you can use Forms authentication to password protect the views in your ASP.NET MVC applications. You learn how to use the Web Site Administration Tool to create users and roles. You also learn how to prevent unauthorized users from invoking controller actions. Finally, you learn how to configure where user names and passwords are stored.

Using the Web Site Administration Tool

Before we do anything else, we should start by creating some users and roles. The easiest way to create new users and roles is to take advantage of the Visual Studio 2008 Web Site Administration Tool. You can launch this tool by selecting the menu option Project, ASP.NET Configuration. Alternatively, you can launch the Web Site Administration Tool by clicking the (somewhat scary) icon of the hammer hitting the world that appears at the top of the Solution Explorer window (see Figure 1).

Figure 1 – Launching the Web Site Administration Tool

clip_image002
clip_image002

Within the Web Site Administration Tool, you create new users and roles by selecting the Security tab. Click the Create user link to create a new user named Stephen (see Figure 2). Provide the Stephen user with any password that you want (for example, secret).

Figure 2 – Creating a new user

clip_image004
clip_image004

You create new roles by first enabling roles and defining one or more roles. Enable roles by clicking the Enable roles link. Next, create a role named Administrators by clicking the Create or Manage roles link (see Figure 3).

Figure 3 – Creating a new role

clip_image006
clip_image006

Finally, create a new user named Sally and associate Sally with the Administrators role by clicking the Create User link and selecting Administrators when creating Sally (see Figure 4).

Figure 4 – Adding a user to a role

clip_image008
clip_image008

When all is said and done, you should have two new users named Stephen and Sally. You should also have a new role named Administrators. Sally is a member of the Administrators role and Stephen is not.

Requiring Authorization

You can require a user to be authenticated before the user invokes a controller action by adding the [Authorize] attribute to the action. You can apply the [Authorize] attribute to an individual controller action or you can apply this attribute to an entire controller class.

For example, the controller in Listing 1 exposes an action named CompanySecrets(). Because this action is decorated with the [Authorize] attribute, this action cannot be invoked unless a user is authenticated.

Listing 1 – Controllers.cs

[!code-csharpMain]

   1:  using System.Web.Mvc;
   2:   
   3:  namespace MvcApplication1.Controllers
   4:  {
   5:      [HandleError]
   6:      public class HomeController : Controller
   7:      {
   8:          public ActionResult Index()
   9:          {
  10:              return View();
  11:          }
  12:          
  13:          [Authorize]
  14:          public ActionResult CompanySecrets()
  15:          {
  16:              return View();
  17:          }
  18:      }
  19:  }

If you invoke the CompanySecrets() action by entering the URL /Home/CompanySecrets in the address bar of your browser, and you are not an authenticated user, then you will be redirected to the Login view automatically (see Figure 5).

Figure 5 – The Login view

clip_image010
clip_image010

You can use the Login view to enter your user name and password. If you are not a registered user then you can click the register link to navigate to the Register view (see Figure 6). You can use the Register view to create a new user account.

Figure 6 – The Register view

clip_image012
clip_image012

After you successfully log in, you can see the CompanySecrets view (see Figure 7). By default, you will continue to be logged in until you close your browser window.

Figure 7 – The CompanySecrets view

clip_image014
clip_image014

Authorizing by User Name or User Role

You can use the [Authorize] attribute to restrict access to a controller action to a particular set of users or a particular set of user roles. For example, the modified Home controller in Listing 2 contains two new actions named StephenSecrets() and AdministratorSecrets().

Listing 2 – Controllers.cs

[!code-csharpMain]

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Web.Mvc;
   6:  using System.Web.Mvc.Ajax;
   7:   
   8:  namespace MvcApplication1.Controllers
   9:  {
  10:      [HandleError]
  11:      public class HomeController : Controller
  12:      {
  13:          public ActionResult Index()
  14:          {
  15:              return View();
  16:          }
  17:   
  18:          
  19:          [Authorize]
  20:          public ActionResult CompanySecrets()
  21:          {
  22:              return View();
  23:          }
  24:   
  25:   
  26:          [Authorize(Users="Stephen")]
  27:          public ActionResult StephenSecrets()
  28:          {
  29:              return View();
  30:          }
  31:   
  32:   
  33:          [Authorize(Roles = "Administrators")]
  34:          public ActionResult AdministratorSecrets()
  35:          {
  36:              return View();
  37:          }
  38:   
  39:      }
  40:  }

Only a user with the user name Stephen can invoke the StephenSecrets() action. All other users get redirected to the Login view. The Users property accepts a comma separated list of user account names.

Only users in the Administrators role can invoke the AdministratorSecrets() action. For example, because Sally is a member of the Administrators group, she can invoke the AdministratorSecrets() action. All other users get redirected to the Login view. The Roles property accepts a comma separated list of role names.

Configuring Authentication

At this point, you might be wondering where the user account and role information is being stored. By default, the information is stored in a (RANU) SQL Express database named ASPNETDB.mdf located in your MVC application’s App_Data folder. This database is generated by the ASP.NET framework automatically when you start using membership.

In order to see the ASPNETDB.mdf database in the Solution Explorer window, you first need to select the menu option Project, Show All Files.

Using the default SQL Express database is fine when developing an application. Most likely, however, you won’t want to use the default ASPNETDB.mdf database for a production application. In that case, you can change where user account information is stored by completing the following two steps:

  1. Add the Application Services database objects to your production database - Change your application connection string to point to your production database

The first step is to add all of the necessary database objects (tables and stored procedures) to your production database. The easiest way to add these objects to a new database is to take advantage of the ASP.NET SQL Server Setup Wizard (see Figure 8). You can launch this tool by opening the Visual Studio 2008 Command Prompt from the Microsoft Visual Studio 2008 program group and executing the following command from the command prompt:

aspnet_regsql

Figure 8 – The ASP.NET SQL Server Setup Wizard

clip_image016
clip_image016

The ASP.NET SQL Server Setup Wizard enables you to select a SQL Server database on your network and install all of the database objects required by the ASP.NET application services. The database server is not required to be located on your local machine.

[!NOTE]

If you don’t want to use the ASP.NET SQL Server Setup Wizard, then you can find SQL scripts for adding the application services database objects in the following folder:

C:.NET.0.50727

After you create the necessary database objects, you need to modify the database connection used by your MVC application. Modify the ApplicationServices connection string in your web configuration (web.config) file so that it points to the production database. For example, the modified connection in Listing 3 points to a database named MyProductionDB (the original ApplicationServices connection string has been commented out).

Listing 3 – Web.config

[!code-xmlMain]

   1:  <connectionStrings>
   2:      <!--<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>-->
   3:    <add name="ApplicationServices" connectionString="data source=localhost;Integrated Security=SSPI;Initial Catalog=MyProductionDB" />
   4:  </connectionStrings>

Configuring Database Permissions

If you use Integrated Security to connect to your database then you will need to add the correct Windows user account as a login to your database. The correct account depends on whether you are using the ASP.NET Development Server or Internet Information Services as your web server. The correct user account also depends on your operating system.

If you are using the ASP.NET Development Server (the default web server used by Visual Studio) then your application executes within the context of your Windows user account. In that case, you need to add your Windows user account as a database server login.

Alternatively, if you are using Internet Information Services then you need to add either the ASPNET account or the NT AUTHORITY/NETWORK SERVICE account as a database server login. If you are using Windows XP then add the ASPNET account as a login to your database. If you are using a more recent operating system, such as Windows Vista or Windows Server 2008, then add the NT AUTHORITY/NETWORK SERVICE account as the database login.

You can add a new user account to your database by using Microsoft SQL Server Management Studio (see Figure 9).

Figure 9 – Creating a new Microsoft SQL Server login

clip_image018
clip_image018

After you create the required login, you need to map the login to a database user with the right database roles. Double-click the login and select the User Mapping tab. Select one or more application services database roles. For example, in order to authenticate users, you need to enable the aspnet_Membership_BasicAccess database role. In order to create new users, you need to enable the aspnet_Membership_FullAccess database role (see Figure 10).

Figure 10 – Adding Application Services database roles

clip_image020
clip_image020

Summary

In this tutorial, you learned how to use Forms authentication when building an ASP.NET MVC application. First, you learned how to create new users and roles by taking advantage of the Web Site Administration Tool. Next, you learned how to use the [Authorize] attribute to prevent unauthorized users from invoking controller actions. Finally, you learned how to configure your MVC application to store user and role information in a production database.

Next



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/mvc/overview/older-versions-1/security/authenticating-users-with-forms-authentication-cs.htm
< THANKS ME>