Skip to content

Commit 003c9a9

Browse files
committed
Auth, OAuth, Roles, Glimpse, Home Cache, Disable Session
1 parent 59eb60e commit 003c9a9

28 files changed

+811
-15
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
## Ignore Visual Studio temporary files, build results, and
22
## files generated by popular Visual Studio add-ons.
33

4+
#files that I added to hide keys
5+
[Cc]onfig/
6+
47
# User-specific files
58
*.suo
69
*.user

Vidly3/App_Start/FilterConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public static void RegisterGlobalFilters(GlobalFilterCollection filters)
1010
filters.Add(new HandleErrorAttribute());
1111
//to apply Authorization globally. allow anonymous where you want unauthenticated viewers
1212
filters.Add(new AuthorizeAttribute());
13+
//prevent access by http after enabling ssl
14+
filters.Add(new RequireHttpsAttribute());
1315
}
1416
}
1517
}

Vidly3/App_Start/Startup.Auth.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Owin.Security.Google;
77
using Owin;
88
using Vidly3.Models;
9+
using Vidly3.Config;
910

1011
namespace Vidly3
1112
{
@@ -54,9 +55,10 @@ public void ConfigureAuth(IAppBuilder app)
5455
// consumerKey: "",
5556
// consumerSecret: "");
5657

57-
//app.UseFacebookAuthentication(
58-
// appId: "",
59-
// appSecret: "");
58+
//use Keys from
59+
app.UseFacebookAuthentication(
60+
appId: Keys.appId,
61+
appSecret: Keys.appSecret);
6062

6163
//app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
6264
//{

Vidly3/Controllers/AccountController.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Web;
77
using System.Web.Mvc;
88
using Microsoft.AspNet.Identity;
9+
using Microsoft.AspNet.Identity.EntityFramework; //import for rolestore
910
using Microsoft.AspNet.Identity.Owin;
1011
using Microsoft.Owin.Security;
1112
using Vidly3.Models;
@@ -151,10 +152,24 @@ public async Task<ActionResult> Register(RegisterViewModel model)
151152
{
152153
if (ModelState.IsValid)
153154
{
154-
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
155+
//change this to use DrivingLicense
156+
var user = new ApplicationUser
157+
{
158+
UserName = model.Email,
159+
Email = model.Email,
160+
DrivingLicense = model.DrivingLicense,
161+
Phone = model.Phone
162+
};
155163
var result = await UserManager.CreateAsync(user, model.Password);
156164
if (result.Succeeded)
157165
{
166+
////next block is temp code for seeding DB. use instead of Seed method
167+
////go to app and register afterwards to add an admin role
168+
//var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext());
169+
//var roleManager = new RoleManager<IdentityRole>(roleStore);
170+
//await roleManager.CreateAsync(new IdentityRole("CanManageMovies"));
171+
//await UserManager.AddToRoleAsync(user.Id, "CanManageMovies");
172+
158173
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
159174

160175
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
@@ -367,7 +382,15 @@ public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmat
367382
{
368383
return View("ExternalLoginFailure");
369384
}
370-
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
385+
//add license and phone fields manually
386+
var user = new ApplicationUser
387+
{
388+
UserName = model.Email,
389+
Email = model.Email,
390+
DrivingLicense = model.DrivingLicense,
391+
Phone = model.Phone
392+
};
393+
371394
var result = await UserManager.CreateAsync(user);
372395
if (result.Succeeded)
373396
{

Vidly3/Controllers/Api/MoviesController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public IHttpActionResult GetMovie(int id)
4040
}
4141

4242
//POST /api/movies
43+
[Authorize(Roles = RoleName.CanManageMovies)]
4344
[HttpPost]
4445
public IHttpActionResult CreateMovie(MovieDto movieDto)
4546
{
@@ -63,6 +64,7 @@ public IHttpActionResult CreateMovie(MovieDto movieDto)
6364

6465
//PUT /api/movies/id
6566
//returns 200. Customer PUT returns 204 because it is void instead of IHttpActionResult
67+
[Authorize(Roles = RoleName.CanManageMovies)]
6668
[HttpPut]
6769
public IHttpActionResult UpdateMovie(int id, MovieDto movieDto)
6870
{
@@ -86,6 +88,7 @@ public IHttpActionResult UpdateMovie(int id, MovieDto movieDto)
8688
}
8789

8890
//DELETE /api/movies/1
91+
[Authorize(Roles = RoleName.CanManageMovies)]
8992
[HttpDelete]
9093
public IHttpActionResult DeleteMovie(int id)
9194
{

Vidly3/Controllers/CustomersController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public ViewResult Index()
115115
//var customers = _context.Customers.Include(c => c.MembershipType).ToList();
116116

117117
//return View(customers);
118+
118119
return View();
119120
}
120121

Vidly3/Controllers/HomeController.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
using System.Linq;
44
using System.Web;
55
using System.Web.Mvc;
6+
using System.Web.UI;
67

78
namespace Vidly3.Controllers
89
{
910
[AllowAnonymous]
1011
public class HomeController : Controller
1112
{
13+
//disable caching for action with [OutputCache(Duration = 0, VaryByParam = "*", NoStore = true)]
14+
15+
//add output cache for datatime in Index view
16+
//time updates every 50 seconds
17+
//Set location to server because it is not specific to a user
18+
//can varybyparam to store different versions of page in cache depending on parameters
19+
//do not prematurely optimize with VaryByParam
20+
[OutputCache(Duration = 50, Location = OutputCacheLocation.Server)] //, VaryByParam = "genre")]
1221
public ActionResult Index()
1322
{
1423
return View();

Vidly3/Controllers/MoviesController.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ protected override void Dispose(bool disposing)
2424
_context.Dispose();
2525
}
2626

27+
//authorize with roles. added the RoleName model so no magic string
28+
[Authorize(Roles = RoleName.CanManageMovies)]
2729
public ActionResult New()
2830
{
2931
var genres = _context.Genres.ToList();
@@ -39,6 +41,7 @@ public ActionResult New()
3941
return View("MovieForm", viewModel);
4042
}
4143

44+
[Authorize(Roles = RoleName.CanManageMovies)]
4245
public ActionResult Edit(int id)
4346
{
4447
var movie = _context.Movies.SingleOrDefault(m => m.Id == id);
@@ -111,7 +114,25 @@ public ViewResult Index()
111114
//var movies = _context.Movies.Include(m => m.Genre).ToList();
112115

113116
// return View(movies);
114-
return View();
117+
118+
119+
//demonstration of memory cache on genres below
120+
//only use for displaying data, not modifying it
121+
//if (MemoryCache.Default["Genres"] == null)
122+
//{
123+
// MemoryCache.Default["Genres"] = _context.Genres.ToList();
124+
//}
125+
126+
//var genres = MemoryCache.Default["Genres"] as IEnumerable<Genre>;
127+
//return View();
128+
129+
130+
//after adding roles we conditionally render different views
131+
//get RoleName from model
132+
if (User.IsInRole(RoleName.CanManageMovies))
133+
return View("Index");
134+
135+
return View("ReadOnlyIndex");
115136
}
116137
//GET Movies/Details/Id
117138
public ActionResult Details(int id)

Vidly3/GlimpseSecurityPolicy.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
// Uncomment this class to provide custom runtime policy for Glimpse
3+
4+
using Glimpse.AspNet.Extensions;
5+
using Glimpse.Core.Extensibility;
6+
7+
namespace Vidly3
8+
{
9+
public class GlimpseSecurityPolicy:IRuntimePolicy
10+
{
11+
public RuntimePolicy Execute(IRuntimePolicyContext policyContext)
12+
{
13+
// You can perform a check like the one below to control Glimpse's permissions within your application.
14+
// More information about RuntimePolicies can be found at http://getglimpse.com/Help/Custom-Runtime-Policy
15+
// var httpContext = policyContext.GetHttpContext();
16+
// if (!httpContext.User.IsInRole("Administrator"))
17+
// {
18+
// return RuntimePolicy.Off;
19+
// }
20+
21+
return RuntimePolicy.On;
22+
}
23+
24+
public RuntimeEvent ExecuteOn
25+
{
26+
// The RuntimeEvent.ExecuteResource is only needed in case you create a security policy
27+
// Have a look at http://blog.getglimpse.com/2013/12/09/protect-glimpse-axd-with-your-custom-runtime-policy/ for more details
28+
get { return RuntimeEvent.EndRequest | RuntimeEvent.ExecuteResource; }
29+
}
30+
}
31+
}
32+
*/

Vidly3/Migrations/201912072156127_SeedUsers.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)