Skip to content

Commit 7477068

Browse files
author
1996dylanriley
committed
Added admin page with authentication.
1 parent 3f80b92 commit 7477068

File tree

12 files changed

+145
-13
lines changed

12 files changed

+145
-13
lines changed

SimpleBlog/App_Start/RouteConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static void RegisterRoutes(RouteCollection routes)
1919
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
2020

2121
routes.MapRoute("Login", "login", new { Controller = "Auth", Action = "login" }, namespaces);
22+
routes.MapRoute("Logout", "logout", new { Controller = "Auth", Action = "Logout" }, namespaces);
2223

2324
routes.MapRoute("Home", "", new {Controller = "Posts", Action = "Index"}, namespaces );
2425
}

SimpleBlog/Areas/Admin/Controllers/PostsController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace SimpleBlog.Areas.Admin.Controllers
88
{
9+
[Authorize(Roles = "admin")] // This tell mvc that you cannot access this page if you are not logged in or your role isn't allow
910
public class PostsController : Controller
1011
{
1112
// GET: Admin/Posts

SimpleBlog/Areas/Admin/Controllers/UsersController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
namespace SimpleBlog.Areas.Admin.Controllers
88
{
9+
[Authorize(Roles = "admin")]
910
public class UsersController : Controller
1011
{
12+
1113
// GET: Admin/Users
1214
public ActionResult Index()
1315
{

SimpleBlog/Controllers/AuthController.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,37 @@
33
using System.Linq;
44
using System.Web;
55
using System.Web.Mvc;
6+
using System.Web.Security;
67
using SimpleBlog.ViewModels;
78

89
namespace SimpleBlog.Controllers
910
{
1011
public class AuthController : Controller
12+
1113
{
14+
public ActionResult Logout()
15+
{
16+
FormsAuthentication.SignOut();
17+
return RedirectToRoute("home");
18+
}
1219
// GET: Auth
1320
public ActionResult Login()
1421
{
1522
return View(new AuthLogin { });
1623
}
1724

1825
[HttpPost]
19-
public ActionResult Login(AuthLogin form)
26+
public ActionResult Login(AuthLogin form, string returnUrl) // the string returnUrl redirects the user to where they want to go if they sign in successfully.
2027
{
2128
if (!ModelState.IsValid) //This tells mvc what to do if the required fields in the model(auth.cs) are invalid.
2229
return View(form); // the form parameter here means retrun the form as it was submitted(the post request).
2330

24-
if (form.UserName != "dylan riley")
25-
{
26-
ModelState.AddModelError("UserName", "Your username proves to me that you are not a boss!");
27-
return View(form);
28-
}
31+
FormsAuthentication.SetAuthCookie(form.Username, true);
32+
33+
if (!string.IsNullOrWhiteSpace(returnUrl))
34+
return Redirect(returnUrl);
2935

30-
return Content("The form is valid");
36+
return RedirectToRoute("home");
3137
}
3238
}
3339
}

SimpleBlog/Controllers/PostsController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace SimpleBlog.Controllers
88
{
99
public class PostsController : Controller
1010
{
11+
1112
public ActionResult Index()
1213
{
1314
return View();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
6+
namespace SimpleBlog.Infrastructure
7+
{
8+
public class RoleProvider : System.Web.Security.RoleProvider
9+
10+
{
11+
public override string ApplicationName
12+
{
13+
get
14+
{
15+
throw new NotImplementedException();
16+
}
17+
18+
set
19+
{
20+
throw new NotImplementedException();
21+
}
22+
}
23+
24+
public override void AddUsersToRoles(string[] usernames, string[] roleNames)
25+
{
26+
throw new NotImplementedException();
27+
}
28+
29+
public override void CreateRole(string roleName)
30+
{
31+
throw new NotImplementedException();
32+
}
33+
34+
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
35+
{
36+
throw new NotImplementedException();
37+
}
38+
39+
public override string[] FindUsersInRole(string roleName, string usernameToMatch)
40+
{
41+
throw new NotImplementedException();
42+
}
43+
44+
public override string[] GetAllRoles()
45+
{
46+
throw new NotImplementedException();
47+
}
48+
49+
public override string[] GetRolesForUser(string username)
50+
{
51+
if (username == "dylan")
52+
return new[] { "admin" };
53+
54+
return new string[] {};
55+
}
56+
57+
public override string[] GetUsersInRole(string roleName)
58+
{
59+
throw new NotImplementedException();
60+
}
61+
62+
public override bool IsUserInRole(string username, string roleName)
63+
{
64+
throw new NotImplementedException();
65+
}
66+
67+
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
68+
{
69+
throw new NotImplementedException();
70+
}
71+
72+
public override bool RoleExists(string roleName)
73+
{
74+
throw new NotImplementedException();
75+
}
76+
}
77+
}

SimpleBlog/SimpleBlog.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
<Compile Include="Global.asax.cs">
153153
<DependentUpon>Global.asax</DependentUpon>
154154
</Compile>
155+
<Compile Include="Infrastructure\RoleProvider.cs" />
155156
<Compile Include="Properties\AssemblyInfo.cs" />
156157
<Compile Include="ViewModels\Auth.cs" />
157158
</ItemGroup>

SimpleBlog/ViewModels/Auth.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SimpleBlog.ViewModels
55
public class AuthLogin
66
{
77
[Required] // this is another annotation
8-
public string UserName { get; set; }
8+
public string Username { get; set; }
99

1010

1111
[Required, DataType(DataType.Password)] // this here is how we tell the view that the html.EditorFor contains password data. This is called

SimpleBlog/Views/Auth/Login.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
{
77
@Html.ValidationSummary() // this here is a really cool feature that return a deafault message if a feild isn't valid.
88
<div>
9-
@Html.LabelFor(x => x.UserName)
10-
@Html.EditorFor(x => x.UserName) <!-- As this is a strongly typed view I can use the -->
9+
@Html.LabelFor(x => x.Username)
10+
@Html.EditorFor(x => x.Username) <!-- As this is a strongly typed view I can use the -->
1111
<!--name username to associate it with the property by the same name in the view model-->
1212
</div>
1313
<div>
1414
@Html.LabelFor(x => x.Password)
15-
<!--- <label>@:Model.Password</label> ->
15+
<!--- <label>(at sign goes here)Model.Password</label> -->
1616

1717
@Html.EditorFor(x => x.Password)
1818
</div>

SimpleBlog/Views/Shared/_Layout.cshtml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,23 @@
1111
</head>
1212
<body>
1313
<div>
14-
<header><h1><a href="@Url.RouteUrl("home")">Simple blog</a></h1></header>
15-
<a href="@Url.RouteUrl("login")">login</a>
14+
<header>
15+
<h1><a href="@Url.RouteUrl("home")">Simple blog</a></h1>
16+
@if (User.Identity.IsAuthenticated)
17+
{
18+
<p> Welcome Back @User.Identity.Name</p>
19+
<a href="@Url.RouteUrl("logout")">Logout</a>
20+
if(User.IsInRole("admin"))
21+
{
22+
<a href="@Url.Action("index", "Posts", new { area = "admin"})">View posts in admin</a> <!-- the reason i put new {area = "admin"} is because the overload requires an object-->
23+
}
24+
}
25+
else
26+
{
27+
<a href="@Url.RouteUrl("login")">login</a>
28+
}
29+
30+
</header>
1631
@RenderBody()
1732

1833
</div>

0 commit comments

Comments
 (0)