BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC -

Slides:



Advertisements
Similar presentations
Nick Feamster CS 6262 Spring 2009
Advertisements

Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Introduction to MVC Adding a View Page NTPCUG Tom Perkins, Ph.D.
Introduction to MVC Action Methods, Edit View, and a Search Feature NTPCUG Dr. Tom Perkins.
Building Modern Websites with ASP.NET Rachel Appel
COMP 321 Week 12. Overview Web Application Security  Authentication  Authorization  Confidentiality Cross-Site Scripting Lab 12-1 Introduction.
1 Chapter 12 Working With Access 2000 on the Internet.
1 The World Wide Web. 2  Web Fundamentals  Pages are defined by the Hypertext Markup Language (HTML) and contain text, graphics, audio, video and software.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
Blackbaud Web Purchasing Training Session 1. Agenda What is Blackbaud Web Purchasing? How to login to Blackbaud Web Purchasing Create a purchase requisition.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Introduction to MVC Adding Model Classes NTPCUG Tom Perkins, Ph.D.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
Lecture 14 – Web Security SFDV3011 – Advanced Web Development 1.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models.
Blackbaud Web Purchasing Training Session 1. Agenda What is Blackbaud Web Purchasing? How to login to Blackbaud Web Purchasing Create a purchase requisition.
Part 06 – A More Complex Data Model Entity Framework and MVC NTPCUG Tom Perkins.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
Tutorial 121 Creating a New Web Forms Page You will find that creating Web Forms is similar to creating traditional Windows applications in Visual Basic.
ASP.NET MVC applications in C# Jim Warren, COMPSCI 280 S Enterprise Software Development.
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
COMP3121 E-Commerce Technologies Richard Henson University of Worcester November 2011.
Chapter 8 Cookies And Security JavaScript, Third Edition.
1 OPOL Training (OrderPro Online) Prepared by Christina Van Metre Independent Educational Consultant CTO, Business Development Team © Training Version.
Introduction to Entity Framework Part 2 CRUD Scaffolding Tom Perkins NTPCUG.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Security Scanners Mark Shtern. Popular attack targets Web – Web platform – Web application Windows OS Mac OS Linux OS Smartphone.
Mark Dixon 1 03 – Passing Data between pages: Forms, Sessions, & Query Strings.
Website Development with PHP and MySQL Saving Data.
Prof Frankl, Spring 2008CS Polytechnic University 1 Overview of Web database applications with PHP.
STATE MANAGEMENT.  Web Applications are based on stateless HTTP protocol which does not retain any information about user requests  The concept of state.
BIT 286: Web Applications Lecture 10 : Thursday, February 5, 2015 ASP.Net Form Submission.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
 Registry itself is easy and straightforward in implementation  The objects of registry are actually complicated to store and manage  Objects of Registry.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
Web Security Lesson Summary ●Overview of Web and security vulnerabilities ●Cross Site Scripting ●Cross Site Request Forgery ●SQL Injection.
ICM – API Server & Forms Gary Ratcliffe.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
1 State and Session Management HTTP is a stateless protocol – it has no memory of prior connections and cannot distinguish one request from another. The.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC -
ASSIGNMENT 2 Salim Malakouti. Ticketing Website  User submits tickets  Admins answer tickets or take appropriate actions.
CSRF Attacks Daniel Chen 11/18/15. What is CSRF?  Cross Site Request Forgery (Sea-Surf)  AKA XSRF/ One Click / Sidejacking / Session Riding  Exploits.
Session Management Tyler Moore CS7403 University of Tulsa Slides adapted in part or whole from Dan Boneh, Stanford CS155 1.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Working with ASP.NET Controls What is ASP.NET Using server controls in your pages Allowing users to create their own accounts Creating a login page Letting.
BIT 286: Web Applications ASP.Net MVC. Objectives Applied MVC overview Controllers Intro to Routing Views ‘Convention over configuration’ Layout files.
17 Copyright © 2006, Oracle. All rights reserved. Information Publisher.
Distributed Web Systems Cookies and Session Tracking Lecturer Department University.
Joomla Awdhesh Kumar Singsys Pte Ltd. What is Joomla? Joomla is an award-winning content management system (CMS), which enables you to build Web sites.
SlideSet #20: Input Validation and Cross-site Scripting Attacks (XSS) SY306 Web and Databases for Cyber Operations.
1 Adding a Model. We have created an MVC web app project Added a controller class. Added a view class. Next we will add some classes for managing movies.
Web Security (cont.) 1. Referral issues r HTTP referer (originally referrer) – HTTP header that designates calling resource  Page on which a link is.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models.
Build Data Driven Apps with ASP.NET Core Rachel Appel.
Jim Fawcett CSE686 – Internet Programming Spring 2014
Web Application Vulnerabilities, Detection Mechanisms, and Defenses
Jim Fawcett CSE686 – Internet Programming Spring 2012
Play Framework: Introduction
Cross-Site Forgery
04 | Customizing Controllers
Controllers.
Lecture 5: Functions and Parameters
Cross Site Request Forgery (CSRF)
Presentation transcript:

BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC -

Examining the Edit Methods and Edit View  first-mvc-app/controller-methods- views.html first-mvc-app/controller-methods- views.html  Using Entity Framework attributes to annotate model fields  Using the Tag helpers (to generate path via routing)  Postbacks  2 nd method with bound object  Redirecting  Anti-forgery token 2

using System; using System.ComponentModel.DataAnnotations; using System.Data.Entity; namespace MvcMovie.Models { public class Movie { public int ID { get; set; } public string Title { get; set; } [Display(Name = "Release Date")] // default was “ReleaseDate” [DataType(DataType.Date)] public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } } public class MovieDBContext : DbContext { public DbSet Movies { get; set; } } Using EF attributes to better define DB table 3

using System; using System.ComponentModel.DataAnnotations; using System.Data.Entity; namespace MvcMovie.Models { public class Movie { public int ID { get; set; } public string Title { get; set; } [Display(Name = "Release Date")] // default was “ReleaseDate” [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } } public class MovieDBContext : DbContext { public DbSet Movies { get; set; } } Using EF attributes to better define DB table 4

C# Attributes (aka “Annotations” in Java)  “C# provides a mechanism for defining declarative tags, called attributes, which you can place on certain entities in your source code to specify additional information.”  “Entities” – classes, methods, instance variables, etc.  “The information that attributes contain can be retrieved at run time through reflection. You can use predefined attributes or you can define your own custom attributes. ” -- MSDNreflectionpredefined attributes MSDN  NUnit used these ( [Test], [TestFixture], [Category], etc) 5

Date formats  There’s a page for the DisplayFormat attributeDisplayFormat  According to the docs, this can usedocs  Various standard formatting codes for C#’s date&time typesstandard formatting codes for C#’s date&time types  Custom formats (This is what the page is using) Custom formats  For example, "{0:dddd, MMMM d, yyyy}" will produce: Friday, January 1,

Tag Helpers 7

MVC 6 Anchor Tag Helper  In Views/Movies/Index.cshtml:  Edit | Details | Delete  This generates:  Edit | Details | Delete 8

MVC 6 Anchor Tag Helper  In Views/Movies/Index.cshtml: Edit This generates: Edit  This actually builds the path based on the routing info in Startup.cs/Configure()  Routes  You can change your routing and all the links will change  You can choose a different controller with asp-controller=“Product”  You can pass parameters via asp-route-  Parameter must be specified in the route (in Startup.cs/Configure()) 9

MVC 6 Anchor Tag Helper: Named Routes  “Another option is to specify the controller and action using a named route. For example, if your application had a route named login defined as follows in your MVC route configuration:  routes.MapRoute( name: "login", template: "login", defaults: new { controller = "Account", action = "Login" });  then you can bind an anchor tag to that route as follows:  Login ” From anchor-tag-helper.aspx#Named_Routeshttp:// anchor-tag-helper.aspx#Named_Routes 10

WE STOPPED HERE on 5/2/

How does the Edit page work?  Go to localhost:xxxx/Movies, then click on the ‘Edit’ link for something  The URL in the browser will be something like  This is done using an HTTP GET request  Change something, click ‘Save’  Browser needs to sends the changed data to the same URL  but this time using a POST request   When the ‘Save’ works you’ll see the Index page again 12

Controllers/MoviesController.cs  Look at // GET: Movies/Edit/5 public IActionResult Edit(int? id) { if (id == null) { return HttpNotFound(); } Movie movie = _context.Movie.Single(m => m.ID == id); if (movie == null) { return HttpNotFound(); } return View(movie); } 13 This is the action that gets executed when a GET request is made

Controllers/MoviesController.cs  Look at // POST: Movies/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit(Movie movie) { if (ModelState.IsValid) { _context.Update(movie); _context.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } 14 This is the action that gets executed when a POST request is made  Could mark the prior method with [HttpGet], but that’s not needed because that’s the default

Controllers/MoviesController.cs  Look at // POST: Movies/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit(Movie movie) { if (ModelState.IsValid) { _context.Update(movie); _context.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } 15 This will prevent Cross-Site Request Forgery (CSRF, aka XSS attack). Specifically, this attribute says to check the anti- forgery token is the value that we gave to the browser (when it asked for the Edit form)

CSRF / XSS Attack  XSS Attack:  Customer logs into your web site, then visits another, malicious web site  Malicious web site knows that the browser is storing login credentials & that it will send appropriate cookies back to your web site when browser next visits that site  Malicious web site creates it’s own form to post to your website, and is logged in with your credentials  Antiforgery Token:  In addition to the cookie, ASP.Net will add a random number to each form (and it will save a copy for it’s later reference)  The number is the token  This is added in via  When a form is submitted ASP.Net checks both  the cookie (which the browser sends automatically)  the token (the random number, which the malicious site cannot get) 16

Controllers/MoviesController.cs  Look at // POST: Movies/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit(Movie movie) { if (ModelState.IsValid) { _context.Update(movie); _context.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } 17 Checks if the information provided is valid This is done in the client for convenience, and in the server for security

Controllers/MoviesController.cs  Look at // POST: Movies/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit(Movie movie) { if (ModelState.IsValid) { _context.Update(movie); _context.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } 18 These lines save the information into the database.

Controllers/MoviesController.cs  Look at // POST: Movies/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit(Movie movie) { if (ModelState.IsValid) { _context.Update(movie); _context.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } 19 This will redirect the browser to the Index URL

Controllers/MoviesController.cs  Look at // POST: Movies/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit(Movie movie) { if (ModelState.IsValid) { _context.Update(movie); _context.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } 20 If the server was given invalid data, then direct the browser back to the Edit page

Older ways of generating links  If you’re using MVC 5 then you don’t need the following, but it’s good to know about since it’s the way this stuff was done in the prior verion(s). 21

The Html helper class  How to link from, say, the Index page to the Details page?  In "Edit", new { id=item.ID }) "Details", new { id=item.ID }) "Delete", new { id=item.ID }) 22

Html.ActionLink method "Details", new { id=item.ID }) |  First arg is the text to put on the page  Second arg is the name of the method on this controller to link to  Movies.Details  Third arg is an anonymous object with info needed to build the path  This actually builds the path based on the routing info in App_Data/RouteConfig.cs  You can change your routing and all the links will change  Details at MSDN Details at MSDN  There are other versions if you want to link to a different controller (e.g., back to the overall home page) 23