add user managment(edit/delete/logout) and session

This commit is contained in:
2024-07-27 17:17:28 +03:30
parent 09f9d07f03
commit 08277427c7
12 changed files with 226 additions and 242 deletions

View File

@ -17,23 +17,33 @@ namespace WebApplication1.Controllers
// GET: users // GET: users
public ActionResult Index() public ActionResult Index()
{ {
return View(db.users.ToList()); if (Session["User"] != null)
{
if (Session["Userid"] == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
user user = db.users.Find(Session["Userid"]);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
else
return RedirectToAction("Login");
}// GET: users
public ActionResult allusers()
{
if (Session["User"] != null)
{
return View(db.users.ToList());
}
else
return RedirectToAction("Login");
} }
// GET: users/Details/5 //registeration
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 // GET: users/Create
public ActionResult Register() public ActionResult Register()
@ -50,28 +60,49 @@ namespace WebApplication1.Controllers
{ {
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
//check if the username exists
var check = db.users.FirstOrDefault(u => u.usename == user.usename); var check = db.users.FirstOrDefault(u => u.usename == user.usename);
//checks username and password field in form
if (user.usename != null && user.password != null) if (user.usename != null && user.password != null)
{ {
if (check != null) if (check != null)
{ {
ModelState.AddModelError("", " username already exists"); ModelState.AddModelError("", " username already exists");
} }
//if username is valid create user add to db
else else
{ {
db.users.Add(user); db.users.Add(user);
db.SaveChanges(); db.SaveChanges();
//session start
Session["User"] = user.usename;
Session["Userid"] = user.ID;
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
} }
else { else
ModelState.AddModelError(""," username and password field can not be empty"); {
} ModelState.AddModelError("",
"username and password field can not be empty");
}
} }
return View(user); return View(user);
} }
// GET: users/logout
public ActionResult Logout()
{
//session check, if exists -> destroy session
if (Session["User"] != null)
{
Session.Abandon();
}
return RedirectToAction("Index");
}
// GET: users/Login // GET: users/Login
public ActionResult Login() public ActionResult Login()
{ {
@ -85,9 +116,14 @@ namespace WebApplication1.Controllers
{ {
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
//check if user exists
var existingUser = db.users.FirstOrDefault(u => u.usename == user.usename ); var existingUser = db.users.FirstOrDefault(u => u.usename == user.usename );
if (existingUser != null && existingUser.password == user.password) if (existingUser != null && existingUser.password == user.password)
{ {
//session start
Session["User"] = existingUser.usename;
Session["Userid"] = existingUser.ID;
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
else else
@ -102,16 +138,22 @@ namespace WebApplication1.Controllers
// GET: users/Edit/5 // GET: users/Edit/5
public ActionResult Edit(int? id) public ActionResult Edit(int? id)
{ {
if (id == null) if (Session["User"] != null)
{ {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
if (Session["Userid"] == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
user user = db.users.Find(Session["Userid"]);
if (user == null)
{
return HttpNotFound();
}
return View(user);
} }
user user = db.users.Find(id); else
if (user == null) return View("Login");
{
return HttpNotFound();
}
return View(user);
} }
// POST: users/Edit/5 // POST: users/Edit/5
@ -135,16 +177,21 @@ namespace WebApplication1.Controllers
// GET: users/Delete/5 // GET: users/Delete/5
public ActionResult Delete(int? id) public ActionResult Delete(int? id)
{ {
if (id == null) if (Session["User"] != null)
{ {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); if (Session["Userid"] == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
user user = db.users.Find(Session["Userid"]);
if (user == null)
{
return HttpNotFound();
}
return View(user);
} }
user user = db.users.Find(id); else
if (user == null) return View("Login");
{
return HttpNotFound();
}
return View(user);
} }
// POST: users/Delete/5 // POST: users/Delete/5

View File

@ -1,26 +0,0 @@

@{
ViewBag.Title = "Login";
}
<h2>Login</h2>
<form asp-action="Login" asp-controller="Account" method="post">
<div asp-validation-summary="All"></div>
<div class="form-group">
<label  
asp-for="usename">username</label>
<input asp-for="usename" class="form-control" required />
<span asp-validation-for="usename" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="password">password</label>
<input asp-for="password"  
type="password" class="form-control" required />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<br />
<button type="submit" class="btn btn-primary">Login</button>
</form>

View File

@ -1,51 +0,0 @@
@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

@ -20,9 +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", "users", new { area = "" }, new { @class = "nav-link" })</li> <li>@Html.ActionLink("user setting", "index", "users", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Register", "Register", "users", new { area = "" }, new { @class = "nav-link" })</li>
</ul> </ul>
</div> </div>
</div> </div>

View File

@ -40,9 +40,11 @@
@using (Html.BeginForm()) { @using (Html.BeginForm()) {
@Html.AntiForgeryToken() @Html.AntiForgeryToken()
<div class="form-actions no-color"> <div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index") @Html.ActionLink("back to account managment", "index", null, new { @class = "btn btn-outline-dark" })
</div>
<input type="submit" value="Delete" class="btn btn-outline-dark" />
</div>
} }
</div> </div>

View File

@ -1,42 +0,0 @@
@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>

View File

@ -1,15 +1,15 @@
@model WebApplication1.Models.user @model WebApplication1.Models.user
@{ @{
ViewBag.Title = "Edit"; ViewBag.Title = "Account managment";
} }
<h2>Account managment</h2>
<h2>Edit</h2> <h4>View and Edit account details</h4>
@using (Html.BeginForm()) @using (Html.BeginForm())
{ {
@Html.AntiForgeryToken() @Html.AntiForgeryToken()
<div class="form-horizontal"> <div class="form-horizontal">
<h4>user</h4> <h4>user</h4>
<hr /> <hr />
@ -19,7 +19,7 @@
<div class="form-group"> <div class="form-group">
@Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10"> <div class="col-md-10">
@Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } }) @Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" }) @Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" })
</div> </div>
</div> </div>
@ -39,15 +39,17 @@
@Html.ValidationMessageFor(model => model.displayname, "", new { @class = "text-danger" }) @Html.ValidationMessageFor(model => model.displayname, "", new { @class = "text-danger" })
</div> </div>
</div> </div>
<br />
<div class="form-group"> <div class="form-group">
<div class="col-md-offset-2 col-md-10"> <div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
@Html.ActionLink("back to account managment", "index", null, new { @class = "btn btn-outline-dark" })
<input type="submit" value="Save" class="btn btn-outline-dark" />
</div> </div>
</div> </div>
</div> </div>
} }
<div>
@Html.ActionLink("Back to List", "Index")
</div>

View File

@ -1,45 +1,53 @@
@model IEnumerable<WebApplication1.Models.user> @model WebApplication1.Models.user
@{ @{
ViewBag.Title = "Index"; ViewBag.Title = "Account managment";
} }
<h2>Account managment</h2>
<h4>View and Edit account details</h4>
<h2>Index</h2> @using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<p> <div>
@Html.ActionLink("Create New", "Create") <h4>user</h4>
</p> <hr />
<table class="table"> <dl class="dl-horizontal">
<tr> <dt>
<th>
@Html.DisplayNameFor(model => model.usename) @Html.DisplayNameFor(model => model.usename)
</th> </dt>
<th>
<dd>
@Html.DisplayFor(model => model.usename)
</dd>
<dt>
@Html.DisplayNameFor(model => model.password) @Html.DisplayNameFor(model => model.password)
</th> </dt>
<th>
<dd>
@Html.DisplayFor(model => model.password)
</dd>
<dt>
@Html.DisplayNameFor(model => model.displayname) @Html.DisplayNameFor(model => model.displayname)
</th> </dt>
<th></th>
</tr>
@foreach (var item in Model) { <dd>
<tr> @Html.DisplayFor(model => model.displayname)
<td> </dd>
@Html.DisplayFor(modelItem => item.usename)
</td> </dl>
<td> <div class="col-md-offset-2 col-md-10">
@Html.DisplayFor(modelItem => item.password) @Html.ActionLink("edit account details", "Edit", null, new { @class = "btn btn-outline-dark" })
</td>
<td> @Html.ActionLink("Logout", "LogOut", null, new { @class = "btn btn-outline-dark" })
@Html.DisplayFor(modelItem => item.displayname) </div>
</td> <br />
<td> <div class="col-md-offset-2 col-md-10">
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) | @Html.ActionLink("Delete Account", "Delete", null, new { @class = "btn btn-outline-dark" })
@Html.ActionLink("Details", "Details", new { id=item.ID }) | </div>
@Html.ActionLink("Delete", "Delete", new { id=item.ID }) </div>
</td> }
</tr>
}
</table>

View File

@ -39,5 +39,5 @@
} }
<div> <div>
@Html.ActionLink("Back to List", "Index") @Html.ActionLink("don't have an account? register now", "Register")
</div> </div>

View File

@ -10,42 +10,44 @@
{ {
@Html.AntiForgeryToken() @Html.AntiForgeryToken()
<div class="form-horizontal"> <div class="form-horizontal">
<h4>user</h4> <h4>user</h4>
<hr /> <hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group"> <div class="form-group">
@Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10"> <div class="col-md-10">
@Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } }) @Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" }) @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> </div>
<div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.PasswordFor(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> <div>
@Html.ActionLink("Back to List", "Index") @Html.ActionLink("already have an account? Login", "Login")
</div> </div>

View File

@ -0,0 +1,45 @@
@model IEnumerable<WebApplication1.Models.user>
@{
ViewBag.Title = "allusers";
}
<h2>allusers</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>

View File

@ -251,14 +251,12 @@
<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\Account\Register.cshtml" />
<Content Include="Views\Account\Login.cshtml" />
<Content Include="Views\users\Register.cshtml" /> <Content Include="Views\users\Register.cshtml" />
<Content Include="Views\users\Delete.cshtml" /> <Content Include="Views\users\Delete.cshtml" />
<Content Include="Views\users\Details.cshtml" />
<Content Include="Views\users\Edit.cshtml" /> <Content Include="Views\users\Edit.cshtml" />
<Content Include="Views\users\Index.cshtml" /> <Content Include="Views\users\Index.cshtml" />
<Content Include="Views\users\Login.cshtml" /> <Content Include="Views\users\Login.cshtml" />
<Content Include="Views\users\allusers.cshtml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="App_Data\" /> <Folder Include="App_Data\" />