Introduction:
Begin with an overview of the .NET MVC framework and its importance in building dynamic and scalable web applications.
Introduce the key concepts that will be covered in the presentation: Attribute Routing, Authentication, and Authorization.
Highlight the significance of these features in enhancing the security, usability, and structure of MVC applications.
Section 1: Attribute Routing in .NET MVC:
Definition and Purpose:
Define Attribute Routing and explain its role in defining routes using attributes directly within the controller and action methods.
Emphasize the benefits of attribute routing in terms of readability, maintainability, and providing fine-grained control over URL patterns.
Syntax and Examples:
Provide examples of attribute routing syntax within controllers and actions.
Demonstrate how attribute routing allows developers to create custom, SEO-friendly, and RESTful URLs.
Showcase scenarios where attribute routing excels over convention-based routing.
Section 2: Authentication in .NET MVC:
Understanding Authentication:
Define Authentication and discuss its importance in verifying the identity of users accessing an application.
Introduce the authentication mechanisms supported by .NET MVC, such as Forms Authentication, Windows Authentication, and OAuth.
Implementing Authentication:
Walk through the process of implementing authentication in .NET MVC using attributes, filters, and middleware.
Discuss the role of the [Authorize] attribute and how it restricts access to specific controllers or actions based on the user's authentication status.
Section 3: Authorization in .NET MVC:
Overview of Authorization:
Define Authorization and distinguish it from authentication.
Emphasize the significance of controlling access to specific resources based on user roles, claims, or other criteria.
Implementing Authorization:
Discuss how authorization can be implemented in .NET MVC using attributes like [Authorize] and [AllowAnonymous].
Explore scenarios where role-based authorization and custom policies are essential.
Provide examples of how to implement role-based access control and attribute-based access control.
Case Studies and Best Practices:
Present real-world case studies or examples showcasing the effective use of attribute routing, authentication, and authorization in .NET MVC projects.
Share best practices for maintaining a secure and well-structured MVC application, including tips on managing user roles, securing sensitive data, and handling authentication cookies.
Conclusion:
Summarize the key takeaways from the presentation.
Reinforce the importance of attribute routing, authentication, and authorization in building robust and secure .NET MVC applications.
Encourage further exploration through resources, documentation, and community forums.
3. MVC Filters
• In ASP.NET MVC, a user request is routed to the appropriate controller and action
method. However, there may be circumstances where you want to execute some logic
before or after an action method executes. ASP.NET MVC provides filters for this
purpose.
• ASP.NET MVC Filter is a custom class where you can write custom logic to execute
before or after an action method executes. Filters can be applied to an action method or
controller
4. • Filters can be applied to an action method or controller
• You could apply filters in a declarative or programmatic way:
• Declarative means by applying a filter attribute to an action method or
controller class
• Programmatic means by implementing a corresponding interface or abstract
class
MVC Filters (Cont.)
5. • The ASP.NET MVC framework supports four different types of filters:
• Authorization Filters − Implements the IAuthorizationFilter attribute.
• Action Filters − Implements the IActionFilter attribute.
• Result Filters − Implements the IResultFilter attribute.
• Exception Filters − Implements the IExceptionFilter attribute.
• Filters are executed in the order listed above. For example, authorization filters are always
executed before action filters and exception filters are always executed after every other type of
filter.
• Authorization filters are used to implement authentication and authorization for controller
actions.
MVC Filters Types
6. MVC Filters Types
• MVC provides different types of filters. The following table list filter types, built-in
filters, and interface that must be implemented to create custom filters.
7. Custom Filters
• To create your own custom filter, ASP.NET MVC framework provides a
base class which is known as ActionFilterAttribute. This class
implements both IActionFilter and IResultFilter interfaces and both are
derived from the Filter class.
8.
9.
10. • Action selector is the attribute that can be applied to the action methods. It
helps the routing engine to select the correct action method to handle a
particular request. MVC 5 includes the following action selector attributes:
• ActionName
• NonAction
• ActionVerbs (AcceptVerbs)
Action Selectors
13. • In ASP.NET Core, routing is handled by routing middleware, which matches the URLs of
incoming requests to actions or other endpoints.
• Controller actions are either conventionally routed or attribute-routed.
• Conventional routing is similar to the route table approach used in ASP.NET MVC and Web
API. Whether you're using conventional, attribute, or both, you need to configure your app to
use the routing middleware.
• To use the middleware, add the following code to your Program.cs file:
• app.UseRouting();
Routing
15. • With conventional routing, you set up one or more conventions that will be used to match incoming
URLs to endpoints in the app.
• In ASP.NET Core, these endpoints may be controller actions, like in ASP.NET MVC or Web API.
• In addition to (UseRouting), this configuration specifies a default routing convention, which is the fairly
standard {controller}/{action}/{id?} pattern that's been recommended since the first versions of
ASP.NET MVC:
• app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Conventional Routing
17. • Apply attribute routing on any action:
• //...../Order/GetOrders/3
• //...../customers/3/Orders
[Route("customers/{customerId}/orders")]
public ActionResult GetOrders(int customerId) {…}
Attribute Routing
18. • RoutePrefix on a controller:
[Route(“Company/Finance")]
• Then on each action in controller you should specify a specific route:
[Route("customers/{id}")]
[Route("")]
RoutePrefix
19. Use a tilde (~) on the method attribute to override the route prefix:
[Route("mvc/books")]
public class BooksController : Controller
{
// GET /mvc/authors/1/books
[Route("~/mvc/authors/{authorId:int}/books")]
public IEnumerable<Book> GetByAuthor(int authorId) { ... }
}
Override RoutePrefix
20. • To apply Route Constraints:
[Route("users/{id:int:min(10)}")]
public User GetUserById(int id) { ... }
Route Constraints
21. Constraint Description Example
Alpha Matches uppercase or lowercase Latin alphabet characters (a-z,
A-Z)
{x:alpha}
Bool Matches a Boolean value. {x:bool}
Datetime Matches a DateTime value. {x:datetime}
Decimal Matches a decimal value. {x:decimal}
Double Matches a 64-bit floating-point value. {x:double}
Float Matches a 32-bit floating-point value. {x:float}
Guid Matches a GUID value. {x:guid}
Int Matches a 32-bit integer value. {x:int}
22. Length Matches a string with the specified length or
within a specified range of lengths.
{x:length(6)}
{x:length(1,20)}
Long Matches a 64-bit integer value. {x:long}
Max Matches an integer with a maximum value. {x:max(10)}
maxlength Matches a string with a maximum length. {x:maxlength(10)}
Min Matches an integer with a minimum value. {x:min(10)}
minlength Matches a string with a minimum length. {x:minlength(10)}
Range Matches an integer within a range of values. {x:range(10,50)}
Regex Matches a regular expression. {x:regex(^d{3}-d{3}-
d{4}$)}
24. ASP.NET Membership
• It was designed to solve site membership requirements that were
common in 2005, which involved Forms Authentication, and a
SQL Server database for user names, passwords, and profile data.
• Today there is a much broader array of data storage options for
web applications, and most developers want to enable their sites to
use social identity providers for authentication and authorization
functionality.
25. ASP.NET Membership Limitations
• The database schema was designed for SQL Server and you can't
change it.
• Membership database is limited to describe users identity.
• Since the log-in/log-out functionality is based on Forms
Authentication, the membership system can't use OWIN.
26. ASP.NET Identity
ASP.NET Identity was developed with the following goals:
• One ASP.NET Identity system
• Persistence control
• Claims Based
• Role provider
• Social Login Providers
• OWIN Integration
• Unit testability
29. Live Accounts Authentication
• You should Choose “Individual User” Authentication MVC template
when creating your web application
• You have to create a project on “Google.developer.console” or what so
ever provider you will use for authentication process delegation
• You need a “Client ID & Client Secret” to be able to use provider
API for authenticating your web application