Author: DevGeek

User Authentication in ASP.NET

The purpose of this article is to explain how to use form authentication to password protect the Views of your ASP.NET MVC applications. You will learn how to use the Website Administration Tool to create users and roles. You will also learn to prevent unauthorized users from invoking controller actions. Finally, you will learn how to configure how user names and passwords are stored. User Authentication in ASP.NET. User Authentication in ASP.NET Use the Website Administration Tool Before doing anything else, we must start by creating a few users and roles. The easiest way to create new users and roles is to take advantage of the Visual Studio 2008 Website Administration Tool. You can run this tool through the Project, ASP.NET Configuration menu. You can also launch the Website Administration Tool by clicking on the (slightly scary) icon of the hammer that hits the world above the Solution Explorer.   Launch the Website Administration Tool In the Website Administration Tool, you can create new users and roles by choosing the Security tab. Click on the “Create User” link to create a new user named Stephen. Assign the user Stephen the password of your choice (for example, “secret”). Create a new user You will be able to create new roles first by activating and then defining them. Activate the roles by clicking on the “Activate roles” link. Then create a role named “Administrators” by clicking on the “Create or...

Read More

How to Create Custom Routes in MVC?

Learn how to create custom routes in MVC application. In this tutorial, you will also learn how to change the default route in the Global.asax file. In this tutorial, you will learn how to add custom routes to an ASP.Net MVC application. In this tutorial, you will also learn how to modify the default route in the Global.asax file using a custom route. For several simple ASP.Net MVC applications, the default route may be appropriate. Conversely, you may discover that you have specialized road needs. In this case, you can create a custom route. Imagine, for example, that you are building a blogging app. So you want to know how to take into account and use URLs that look like this: / Archive / 12-25-2009 When a user enters this query, you want to return the blog entry that matches the date 12/25/2009. To support this type of quest, you must create a custom route. The following Global.asax file contains a new custom route, named Blog, that supports requests of the form / Archive / date. Here’s Global.asax using System.Web.Mvc; using System.Web.Routing; namespace MvcApplication1 { public class MvcApplication : System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Blog", // Route name "Archive/{entryDate}", // URL with parameters new { controller = "Archive", action = "Entry" } // Parameter defaults ); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new {controller = "Home",...

Read More

How to Use Simple Validations in ASP.NET MVC?

The purpose of this tutorial is to explain how you can perform validations within an ASP.NET MVC application. For example, you will learn how to prevent someone from submitting a form when the form does not have a value for a required field. You will learn how to use simple validations in asp.net mvc (the State Model and HTML Helper validation methods). So, how to use simple Validations in ASP.NET MVC? Understand the state model You use the state model – or, more precisely, the state model dictionary – to represent validation errors. For example, the ActionResult Create () validates the properties of a product class ( Product ) before adding the product to a database. Following is the ProductController. // POST: /Product/Create [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude="Id")] Product productToCreate) { // Logique de validation if (productToCreate.Name.Trim().Length == 0) ModelState.AddModelError("Name", "Name is required."); if (productToCreate.Description.Trim().Length == 0) ModelState.AddModelError("Description", "Description is required."); if (productToCreate.UnitsInStock < 0) ModelState.AddModelError("UnitsInStock", "Units in stock cannot be less than zero."); if (!ModelState.IsValid) return View(); // Logique de la base de données try { _entities.AddToProductSet(productToCreate); _entities.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } } Name, description and unit stock of the Product class are validated. If any of these properties fail the validation test, an error message is added to the state model dictionary (represented by the ModelState property of the controller class). If there are errors in the state model, then the ModelState.IsValid property returns false ( false ). In this case,...

Read More

Deploy ASP.NET MVC on IIS – MVC Help

The ASP.NET MVC framework relies on ASP.NET routing to route requests from the browser to controller actions. In order to take advantage of ASP.NET routing, you may need to change the configuration of your web server. In fact, it all depends on the version of your IIS and how your application handles queries. There’s also a question how to deploy asp.net mvc on IIS. This article is just answer to this question. Here is a summary of the different versions of IIS: – IIS 7.0 (integrated world) – No special configuration is needed to use ASP.NET routing – IIS 7.0 (classic mode) – You need to make some configuration changes to use the ASP.NET routing – IIS 6.0 or less – You need to make some configuration changes to use ASP.NET routing The latest version of IIS is version 7.0. This version of IIS is included in Windows Server 2008. You can also install IIS 7.0 on any version of Vista except the Basic Home version (see http://technet.microsoft.com/en-us). /library/cc732624.aspx) IIS 7.0 supports 2 modes of query processing. You can use the integrated mode or the classic mode. You do not need to make any configuration changes if you are using IIS 7.0 in integrated mode. However, you must do this if you are using IIS 7.0 in Classic mode. Microsoft Windows Server 2003 contains IIS 6.0. You can not install IIS 7.0 on a Windows Server...

Read More

Code First Entity Framework – ASP.NET MVC TUTORIALS

Entity Framework is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. Entity framework have two approaches to work. 1 is database first and 2nd is code first approach. In this tutorial, we’ll discuss about code first entity framework. Code First Entity Framework Now we know about code first entity framework approach in asp.net mvc. In the next tutorial, we’ll discuss about seeding the...

Read More