set up login and register page

This commit is contained in:
2024-07-27 11:14:43 +03:30
parent 9073475fb0
commit 09f9d07f03
18 changed files with 540 additions and 116 deletions

View File

@ -55,30 +55,44 @@ namespace WebApplication1.Controllers
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
// Replace this with actual user creation logic, including password hashing // Replace this with actual user creation logic, including password hashing
// and storing user information in a database // and storing user information in a database
/*var allusers = db.users.ToList(); /*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"); ModelState.AddModelError("", "username already exists");
break; return RedirectToAction("Register");
} }
}*/ }*/
/*db.users.Add(new user db.users.Add( new user
{ {
usename = model.username, usename = model.username,
password = model.password, password = model.password,
displayname = model.displayname displayname = model.displayname
}); });
db.SaveChanges(); db.SaveChanges();
}*/ return RedirectToAction("Home","Index");
return RedirectToAction("Login");
} }
/*db.users.Add(new user
{
usename = model.username,
password = model.password,
displayname = model.displayname
});
db.SaveChanges();
}*/
// return RedirectToAction("Login");
return View(model); return View(model);
} }
} }

View File

@ -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();
}
}
}

View File

@ -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);
}
}
}

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Web; using System.Web;
@ -7,6 +8,8 @@ namespace WebApplication1.Models
{ {
public class Login public class Login
{ {
[Key]
public int ID { get; set; }
public string username { get; set; } public string username { get; set; }
public string password { get; set; } public string password { get; set; }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Web; using System.Web;
@ -7,6 +8,8 @@ namespace WebApplication1.Models
{ {
public class Register public class Register
{ {
[Key]
public int ID { get; set; }
public string username { get; set; } public string username { get; set; }
public string password { get; set; } public string password { get; set; }
public string ConfirmPassword { get; set; } public string ConfirmPassword { get; set; }

View File

@ -9,9 +9,9 @@
<div asp-validation-summary="All"></div> <div asp-validation-summary="All"></div>
<div class="form-group"> <div class="form-group">
<label   <label  
asp-for="username">username</label> asp-for="usename">username</label>
<input asp-for="username" class="form-control" required /> <input asp-for="usename" class="form-control" required />
<span asp-validation-for="Username" class="text-danger"></span> <span asp-validation-for="usename" class="text-danger"></span>
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="password">password</label> <label asp-for="password">password</label>

View File

@ -1,56 +1,51 @@
 @model WebApplication1.Models.user
@{ @{
ViewBag.Title = "Register"; ViewBag.Title = "Register";
} }
<h2>Register</h2> <h2>Create a new user</h2>
<script> @using (Html.BeginForm())
function checkPasswords() { {
var password = document.getElementById("password"); @Html.AntiForgeryToken()
var confirmPassword = document.getElementById("ConfirmPassword");  
var errorMessage = document.getElementById("passwordMismatch");
<div class="form-horizontal">
<h4>user</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" })
</div>
</div>
if (password.value !== confirmPassword.value) { <div class="form-group">
errorMessage.textContent = "Passwords do not match."; @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
return false; <div class="col-md-10">
} @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
else { @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
errorMessage.textContent = ""; </div>
return true; </div>
}
<div class="form-group">
@Html.LabelFor(model => model.displayname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.displayname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.displayname, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-outline-dark" />
</div>
</div>
</div>
} }
</script>
<form asp-action="Register" asp-controller="Account" method="post">
<div asp-validation-summary="All"></div>
<div class="form-group">
<label  
asp-for="username"> username </label>
<input asp-for="username" class="form-control" required />
<span asp-validation-for="username" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="password"> password </label>
<input asp-for="password" id="password" 
type="password" class="form-control" required/>
<span asp-validation-for="password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword">Confirm Password</label>
<input asp-for="ConfirmPassword" id="ConfirmPassword"
type="password" class="form-control" oninput="checkPasswords()" required />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
<span id="passwordMismatch" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="displayname">display name</label>
<input asp-for="displayname" class="form-control" required />
<span asp-validation-for="displayname" class="text-danger" ></span>
</div>
<br />
<button type="submit" class="btn btn-primary">Register</button>
</form>
<div>
@Html.ActionLink("Back to List", "Index")
</div>

View File

@ -20,8 +20,8 @@
<li>@Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Login", "Login", "Account", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("Login", "Login", "users", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Register", "Register", "Account", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("Register", "Register", "users", new { area = "" }, new { @class = "nav-link" })</li>
</ul> </ul>
</div> </div>

View File

@ -1,7 +0,0 @@

@{
ViewBag.Title = "Latest";
}
<h2>Latest</h2>

View File

@ -1,7 +0,0 @@

@{
ViewBag.Title = "index";
}
<h2>Whats good?</h2>

View File

@ -1,14 +0,0 @@

@{
ViewBag.Title = "submit";
}
<h2>submit a new article</h2>
<h3>what's new?'</h3>
<form>
title: <input /> <br /><br />
link: <input /> <br /><br />
summary: <input /> <br /><br />
content: <textarea> </textarea> <br /><br />
<input type="submit" />
</form>

48
Views/users/Delete.cshtml Normal file
View File

@ -0,0 +1,48 @@
@model WebApplication1.Models.user
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>user</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.usename)
</dt>
<dd>
@Html.DisplayFor(model => model.usename)
</dd>
<dt>
@Html.DisplayNameFor(model => model.password)
</dt>
<dd>
@Html.DisplayFor(model => model.password)
</dd>
<dt>
@Html.DisplayNameFor(model => model.displayname)
</dt>
<dd>
@Html.DisplayFor(model => model.displayname)
</dd>
</dl>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index")
</div>
}
</div>

View File

@ -0,0 +1,42 @@
@model WebApplication1.Models.user
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>user</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.usename)
</dt>
<dd>
@Html.DisplayFor(model => model.usename)
</dd>
<dt>
@Html.DisplayNameFor(model => model.password)
</dt>
<dd>
@Html.DisplayFor(model => model.password)
</dd>
<dt>
@Html.DisplayNameFor(model => model.displayname)
</dt>
<dd>
@Html.DisplayFor(model => model.displayname)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
@Html.ActionLink("Back to List", "Index")
</p>

53
Views/users/Edit.cshtml Normal file
View File

@ -0,0 +1,53 @@
@model WebApplication1.Models.user
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>user</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.displayname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.displayname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.displayname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>

45
Views/users/Index.cshtml Normal file
View File

@ -0,0 +1,45 @@
@model IEnumerable<WebApplication1.Models.user>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.usename)
</th>
<th>
@Html.DisplayNameFor(model => model.password)
</th>
<th>
@Html.DisplayNameFor(model => model.displayname)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.usename)
</td>
<td>
@Html.DisplayFor(modelItem => item.password)
</td>
<td>
@Html.DisplayFor(modelItem => item.displayname)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>

43
Views/users/Login.cshtml Normal file
View File

@ -0,0 +1,43 @@
@model WebApplication1.Models.user
@{
ViewBag.Title = "Login";
}
<h2>Login</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>user</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Login!" class="btn btn-outline-dark" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>

View File

@ -0,0 +1,51 @@
@model WebApplication1.Models.user
@{
ViewBag.Title = "Register";
}
<h2>Create a new user</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>user</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.displayname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.displayname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.displayname, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-outline-dark" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>

View File

@ -144,7 +144,7 @@
<Compile Include="App_Start\WebApiConfig.cs" /> <Compile Include="App_Start\WebApiConfig.cs" />
<Compile Include="Controllers\AccountController.cs" /> <Compile Include="Controllers\AccountController.cs" />
<Compile Include="Controllers\HomeController.cs" /> <Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\TheNewsController.cs" /> <Compile Include="Controllers\usersController.cs" />
<Compile Include="Global.asax.cs"> <Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon> <DependentUpon>Global.asax</DependentUpon>
</Compile> </Compile>
@ -251,11 +251,14 @@
<Content Include="Views\Home\About.cshtml" /> <Content Include="Views\Home\About.cshtml" />
<Content Include="Views\Home\Contact.cshtml" /> <Content Include="Views\Home\Contact.cshtml" />
<Content Include="Views\Home\Index.cshtml" /> <Content Include="Views\Home\Index.cshtml" />
<Content Include="Views\TheNews\Latest.cshtml" />
<Content Include="Views\TheNews\index.cshtml" />
<Content Include="Views\TheNews\submit.cshtml" />
<Content Include="Views\Account\Register.cshtml" /> <Content Include="Views\Account\Register.cshtml" />
<Content Include="Views\Account\Login.cshtml" /> <Content Include="Views\Account\Login.cshtml" />
<Content Include="Views\users\Register.cshtml" />
<Content Include="Views\users\Delete.cshtml" />
<Content Include="Views\users\Details.cshtml" />
<Content Include="Views\users\Edit.cshtml" />
<Content Include="Views\users\Index.cshtml" />
<Content Include="Views\users\Login.cshtml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="App_Data\" /> <Folder Include="App_Data\" />