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
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
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);
}
//registeration
// GET: users/Create
public ActionResult Register()
@ -50,28 +60,49 @@ namespace WebApplication1.Controllers
{
if (ModelState.IsValid)
{
//check if the username exists
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 (check != null)
{
ModelState.AddModelError("", " username already exists");
}
//if username is valid create user add to db
else
{
db.users.Add(user);
db.SaveChanges();
//session start
Session["User"] = user.usename;
Session["Userid"] = user.ID;
return RedirectToAction("Index");
}
}
else {
ModelState.AddModelError(""," username and password field can not be empty");
}
else
{
ModelState.AddModelError("",
"username and password field can not be empty");
}
}
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
public ActionResult Login()
{
@ -85,9 +116,14 @@ namespace WebApplication1.Controllers
{
if (ModelState.IsValid)
{
//check if user exists
var existingUser = db.users.FirstOrDefault(u => u.usename == user.usename );
if (existingUser != null && existingUser.password == user.password)
{
//session start
Session["User"] = existingUser.usename;
Session["Userid"] = existingUser.ID;
return RedirectToAction("Index");
}
else
@ -102,16 +138,22 @@ namespace WebApplication1.Controllers
// GET: users/Edit/5
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);
if (user == null)
{
return HttpNotFound();
}
return View(user);
else
return View("Login");
}
// POST: users/Edit/5
@ -135,16 +177,21 @@ namespace WebApplication1.Controllers
// GET: users/Delete/5
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);
if (user == null)
{
return HttpNotFound();
}
return View(user);
else
return View("Login");
}
// 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,8 +20,7 @@
<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("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("Register", "Register", "users", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("user setting", "index", "users", new { area = "" }, new { @class = "nav-link" })</li>
</ul>
</div>

View File

@ -40,9 +40,11 @@
@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 class="form-actions no-color">
@Html.ActionLink("back to account managment", "index", null, new { @class = "btn btn-outline-dark" })
<input type="submit" value="Delete" class="btn btn-outline-dark" />
</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,10 +1,10 @@
@model WebApplication1.Models.user
@{
ViewBag.Title = "Edit";
ViewBag.Title = "Account managment";
}
<h2>Edit</h2>
<h2>Account managment</h2>
<h4>View and Edit account details</h4>
@using (Html.BeginForm())
{
@ -19,7 +19,7 @@
<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.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" })
</div>
</div>
@ -39,15 +39,17 @@
@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="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>
@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>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
<div>
<h4>user</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.usename)
</th>
<th>
</dt>
<dd>
@Html.DisplayFor(model => model.usename)
</dd>
<dt>
@Html.DisplayNameFor(model => model.password)
</th>
<th>
</dt>
<dd>
@Html.DisplayFor(model => model.password)
</dd>
<dt>
@Html.DisplayNameFor(model => model.displayname)
</th>
<th></th>
</tr>
</dt>
@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>
}
<dd>
@Html.DisplayFor(model => model.displayname)
</dd>
</dl>
<div class="col-md-offset-2 col-md-10">
@Html.ActionLink("edit account details", "Edit", null, new { @class = "btn btn-outline-dark" })
@Html.ActionLink("Logout", "LogOut", null, new { @class = "btn btn-outline-dark" })
</div>
<br />
<div class="col-md-offset-2 col-md-10">
@Html.ActionLink("Delete Account", "Delete", null, new { @class = "btn btn-outline-dark" })
</div>
</div>
}
</table>

View File

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

View File

@ -10,42 +10,44 @@
{
@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 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.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>
@Html.ActionLink("Back to List", "Index")
@Html.ActionLink("already have an account? Login", "Login")
</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\Contact.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\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" />
<Content Include="Views\users\allusers.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />