From 09f9d07f031cd468e9c0d79413678bce20929641 Mon Sep 17 00:00:00 2001 From: lianpi3 Date: Sat, 27 Jul 2024 11:14:43 +0330 Subject: [PATCH] set up login and register page --- Controllers/AccountController.cs | 42 +++++--- Controllers/TheNewsController.cs | 18 ---- Controllers/usersController.cs | 170 +++++++++++++++++++++++++++++++ Models/Login.cs | 3 + Models/Register.cs | 3 + Views/Account/Login.cshtml | 6 +- Views/Account/Register.cshtml | 89 ++++++++-------- Views/Shared/_Layout.cshtml | 4 +- Views/TheNews/Latest.cshtml | 7 -- Views/TheNews/index.cshtml | 7 -- Views/TheNews/submit.cshtml | 14 --- Views/users/Delete.cshtml | 48 +++++++++ Views/users/Details.cshtml | 42 ++++++++ Views/users/Edit.cshtml | 53 ++++++++++ Views/users/Index.cshtml | 45 ++++++++ Views/users/Login.cshtml | 43 ++++++++ Views/users/Register.cshtml | 51 ++++++++++ WebApplication1.csproj | 11 +- 18 files changed, 540 insertions(+), 116 deletions(-) delete mode 100644 Controllers/TheNewsController.cs create mode 100644 Controllers/usersController.cs delete mode 100644 Views/TheNews/Latest.cshtml delete mode 100644 Views/TheNews/index.cshtml delete mode 100644 Views/TheNews/submit.cshtml create mode 100644 Views/users/Delete.cshtml create mode 100644 Views/users/Details.cshtml create mode 100644 Views/users/Edit.cshtml create mode 100644 Views/users/Index.cshtml create mode 100644 Views/users/Login.cshtml create mode 100644 Views/users/Register.cshtml diff --git a/Controllers/AccountController.cs b/Controllers/AccountController.cs index 15e1bbe..256cdd5 100644 --- a/Controllers/AccountController.cs +++ b/Controllers/AccountController.cs @@ -55,30 +55,44 @@ namespace WebApplication1.Controllers if (ModelState.IsValid) { + // Replace this with actual user creation logic, including password hashing // and storing user information in a database /*var allusers = db.users.ToList(); - - foreach (var nuser in allusers) + + foreach (var user in allusers) + { - if (model.username == nuser.ToString()) + if (model.username.ToString() == user.usename.ToString()) { ModelState.AddModelError("", "username already exists"); - break; + return RedirectToAction("Register"); } }*/ - /*db.users.Add(new user - { - usename = model.username, - password = model.password, - displayname = model.displayname - }); - db.SaveChanges(); - }*/ - - return RedirectToAction("Login"); + db.users.Add( new user + { + usename = model.username, + password = model.password, + displayname = model.displayname + }); + db.SaveChanges(); + return RedirectToAction("Home","Index"); + } + + + /*db.users.Add(new user + { + usename = model.username, + password = model.password, + displayname = model.displayname + }); + db.SaveChanges(); + }*/ + + // return RedirectToAction("Login"); + return View(model); } } diff --git a/Controllers/TheNewsController.cs b/Controllers/TheNewsController.cs deleted file mode 100644 index c4df8ae..0000000 --- a/Controllers/TheNewsController.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Web.Mvc; -using WebApplication1.Models; - -namespace WebApplication1.Controllers -{ - public class TheNewsController : Controller - { - // GET: TheNews - public ActionResult submit() - { - return View(); - } - } -} \ No newline at end of file diff --git a/Controllers/usersController.cs b/Controllers/usersController.cs new file mode 100644 index 0000000..1b449ef --- /dev/null +++ b/Controllers/usersController.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Entity; +using System.Linq; +using System.Net; +using System.Web; +using System.Web.Mvc; +using WebApplication1.Models; + +namespace WebApplication1.Controllers +{ + public class usersController : Controller + { + private newswebappEntities db = new newswebappEntities(); + + // GET: users + public ActionResult Index() + { + return View(db.users.ToList()); + } + + // GET: users/Details/5 + public ActionResult Details(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + user user = db.users.Find(id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); + } + + // GET: users/Create + public ActionResult Register() + { + return View(); + } + + // POST: users/Create + // To protect from overposting attacks, enable the specific properties you want to bind to, for + // more details see https://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Register([Bind(Include = "ID,usename,password,displayname")] user user) + { + if (ModelState.IsValid) + { + var check = db.users.FirstOrDefault(u => u.usename == user.usename); + if (user.usename != null && user.password != null) + { + if (check != null) + { + ModelState.AddModelError("", " username already exists"); + } + else + { + db.users.Add(user); + db.SaveChanges(); + return RedirectToAction("Index"); + } + } + else { + ModelState.AddModelError(""," username and password field can not be empty"); + } + } + + return View(user); + } + + // GET: users/Login + public ActionResult Login() + { + return View(); + } + + // POST: users/Login + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Login([Bind(Include = "usename,password")] user user) + { + if (ModelState.IsValid) + { + var existingUser = db.users.FirstOrDefault(u => u.usename == user.usename ); + if (existingUser != null && existingUser.password == user.password) + { + return RedirectToAction("Index"); + } + else + { + ModelState.AddModelError("", "Invalid username or password"); + } + } + + return View(user); + } + + // GET: users/Edit/5 + public ActionResult Edit(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + user user = db.users.Find(id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); + } + + // POST: users/Edit/5 + // To protect from overposting attacks, enable the specific properties you want to bind to, for + // more details see https://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Edit([Bind(Include = "ID,usename,password,displayname")] user user) + { + if (ModelState.IsValid) + { + db.Entry(user).State = EntityState.Modified; + db.SaveChanges(); + return RedirectToAction("Index"); + } + return View(user); + } + + + + // GET: users/Delete/5 + public ActionResult Delete(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + user user = db.users.Find(id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); + } + + // POST: users/Delete/5 + [HttpPost, ActionName("Delete")] + [ValidateAntiForgeryToken] + public ActionResult DeleteConfirmed(int id) + { + user user = db.users.Find(id); + db.users.Remove(user); + db.SaveChanges(); + return RedirectToAction("Index"); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + db.Dispose(); + } + base.Dispose(disposing); + } + } +} diff --git a/Models/Login.cs b/Models/Login.cs index 362522d..325f3da 100644 --- a/Models/Login.cs +++ b/Models/Login.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; @@ -7,6 +8,8 @@ namespace WebApplication1.Models { public class Login { + [Key] + public int ID { get; set; } public string username { get; set; } public string password { get; set; } } diff --git a/Models/Register.cs b/Models/Register.cs index 646b315..9d585ee 100644 --- a/Models/Register.cs +++ b/Models/Register.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; @@ -7,6 +8,8 @@ namespace WebApplication1.Models { public class Register { + [Key] + public int ID { get; set; } public string username { get; set; } public string password { get; set; } public string ConfirmPassword { get; set; } diff --git a/Views/Account/Login.cshtml b/Views/Account/Login.cshtml index 9a41fb3..e97d1e5 100644 --- a/Views/Account/Login.cshtml +++ b/Views/Account/Login.cshtml @@ -9,9 +9,9 @@
- - + asp-for="usename">username + +
diff --git a/Views/Account/Register.cshtml b/Views/Account/Register.cshtml index 75811e0..6499c5d 100644 --- a/Views/Account/Register.cshtml +++ b/Views/Account/Register.cshtml @@ -1,56 +1,51 @@ - +@model WebApplication1.Models.user + @{ ViewBag.Title = "Register"; } -

Register

+

Create a new user

- -
-
-
- - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- -
+
+ @Html.ActionLink("Back to List", "Index") +
diff --git a/Views/Shared/_Layout.cshtml b/Views/Shared/_Layout.cshtml index 27e6977..90e1b63 100644 --- a/Views/Shared/_Layout.cshtml +++ b/Views/Shared/_Layout.cshtml @@ -20,8 +20,8 @@
  • @Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "nav-link" })
  • @Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })
  • @Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })
  • -
  • @Html.ActionLink("Login", "Login", "Account", new { area = "" }, new { @class = "nav-link" })
  • -
  • @Html.ActionLink("Register", "Register", "Account", new { area = "" }, new { @class = "nav-link" })
  • +
  • @Html.ActionLink("Login", "Login", "users", new { area = "" }, new { @class = "nav-link" })
  • +
  • @Html.ActionLink("Register", "Register", "users", new { area = "" }, new { @class = "nav-link" })
  • diff --git a/Views/TheNews/Latest.cshtml b/Views/TheNews/Latest.cshtml deleted file mode 100644 index b78d637..0000000 --- a/Views/TheNews/Latest.cshtml +++ /dev/null @@ -1,7 +0,0 @@ - -@{ - ViewBag.Title = "Latest"; -} - -

    Latest

    - diff --git a/Views/TheNews/index.cshtml b/Views/TheNews/index.cshtml deleted file mode 100644 index bdb4dd4..0000000 --- a/Views/TheNews/index.cshtml +++ /dev/null @@ -1,7 +0,0 @@ - -@{ - ViewBag.Title = "index"; -} - -

    Whats good?

    - diff --git a/Views/TheNews/submit.cshtml b/Views/TheNews/submit.cshtml deleted file mode 100644 index a580c53..0000000 --- a/Views/TheNews/submit.cshtml +++ /dev/null @@ -1,14 +0,0 @@ - -@{ - ViewBag.Title = "submit"; -} - -

    submit a new article

    -

    what's new?'

    -
    - title:

    - link:

    - summary:

    - content:

    - -
    diff --git a/Views/users/Delete.cshtml b/Views/users/Delete.cshtml new file mode 100644 index 0000000..a529c8c --- /dev/null +++ b/Views/users/Delete.cshtml @@ -0,0 +1,48 @@ +@model WebApplication1.Models.user + +@{ + ViewBag.Title = "Delete"; +} + +

    Delete

    + +

    Are you sure you want to delete this?

    +
    +

    user

    +
    +
    +
    + @Html.DisplayNameFor(model => model.usename) +
    + +
    + @Html.DisplayFor(model => model.usename) +
    + +
    + @Html.DisplayNameFor(model => model.password) +
    + +
    + @Html.DisplayFor(model => model.password) +
    + +
    + @Html.DisplayNameFor(model => model.displayname) +
    + +
    + @Html.DisplayFor(model => model.displayname) +
    + +
    + + @using (Html.BeginForm()) { + @Html.AntiForgeryToken() + +
    + | + @Html.ActionLink("Back to List", "Index") +
    + } +
    diff --git a/Views/users/Details.cshtml b/Views/users/Details.cshtml new file mode 100644 index 0000000..3956034 --- /dev/null +++ b/Views/users/Details.cshtml @@ -0,0 +1,42 @@ +@model WebApplication1.Models.user + +@{ + ViewBag.Title = "Details"; +} + +

    Details

    + +
    +

    user

    +
    +
    +
    + @Html.DisplayNameFor(model => model.usename) +
    + +
    + @Html.DisplayFor(model => model.usename) +
    + +
    + @Html.DisplayNameFor(model => model.password) +
    + +
    + @Html.DisplayFor(model => model.password) +
    + +
    + @Html.DisplayNameFor(model => model.displayname) +
    + +
    + @Html.DisplayFor(model => model.displayname) +
    + +
    +
    +

    + @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) | + @Html.ActionLink("Back to List", "Index") +

    diff --git a/Views/users/Edit.cshtml b/Views/users/Edit.cshtml new file mode 100644 index 0000000..2dbd952 --- /dev/null +++ b/Views/users/Edit.cshtml @@ -0,0 +1,53 @@ +@model WebApplication1.Models.user + +@{ + ViewBag.Title = "Edit"; +} + +

    Edit

    + +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + +
    +

    user

    +
    + @Html.ValidationSummary(true, "", new { @class = "text-danger" }) + @Html.HiddenFor(model => model.ID) + +
    + @Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" }) +
    +
    + +
    + @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" }) +
    +
    + +
    + @Html.LabelFor(model => model.displayname, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.displayname, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.displayname, "", new { @class = "text-danger" }) +
    +
    + +
    +
    + +
    +
    +
    +} + +
    + @Html.ActionLink("Back to List", "Index") +
    diff --git a/Views/users/Index.cshtml b/Views/users/Index.cshtml new file mode 100644 index 0000000..53f1d1f --- /dev/null +++ b/Views/users/Index.cshtml @@ -0,0 +1,45 @@ +@model IEnumerable + +@{ + ViewBag.Title = "Index"; +} + +

    Index

    + +

    + @Html.ActionLink("Create New", "Create") +

    + + + + + + + + +@foreach (var item in Model) { + + + + + + +} + +
    + @Html.DisplayNameFor(model => model.usename) + + @Html.DisplayNameFor(model => model.password) + + @Html.DisplayNameFor(model => model.displayname) +
    + @Html.DisplayFor(modelItem => item.usename) + + @Html.DisplayFor(modelItem => item.password) + + @Html.DisplayFor(modelItem => item.displayname) + + @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | + @Html.ActionLink("Details", "Details", new { id=item.ID }) | + @Html.ActionLink("Delete", "Delete", new { id=item.ID }) +
    diff --git a/Views/users/Login.cshtml b/Views/users/Login.cshtml new file mode 100644 index 0000000..7435db3 --- /dev/null +++ b/Views/users/Login.cshtml @@ -0,0 +1,43 @@ +@model WebApplication1.Models.user + +@{ + ViewBag.Title = "Login"; +} + +

    Login

    + +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + +
    +

    user

    +
    + @Html.ValidationSummary(true, "", new { @class = "text-danger" }) +
    + @Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" }) +
    +
    + +
    + @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" }) +
    +
    +
    +
    +
    + +
    +
    +
    +} + +
    + @Html.ActionLink("Back to List", "Index") +
    diff --git a/Views/users/Register.cshtml b/Views/users/Register.cshtml new file mode 100644 index 0000000..7d92fa1 --- /dev/null +++ b/Views/users/Register.cshtml @@ -0,0 +1,51 @@ +@model WebApplication1.Models.user + +@{ + ViewBag.Title = "Register"; +} + +

    Create a new user

    + +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + +
    +

    user

    +
    + @Html.ValidationSummary(true, "", new { @class = "text-danger" }) +
    + @Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" }) +
    +
    + +
    + @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" }) +
    +
    + +
    + @Html.LabelFor(model => model.displayname, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.displayname, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.displayname, "", new { @class = "text-danger" }) +
    +
    +
    +
    +
    + +
    +
    +
    +} + +
    + @Html.ActionLink("Back to List", "Index") +
    diff --git a/WebApplication1.csproj b/WebApplication1.csproj index 08ee738..f4191f0 100644 --- a/WebApplication1.csproj +++ b/WebApplication1.csproj @@ -144,7 +144,7 @@ - + Global.asax @@ -251,11 +251,14 @@ - - - + + + + + +