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)
{
// 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);
}
}

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