+ Inizio del register

This commit is contained in:
DawitG96
2018-12-14 16:39:45 +01:00
parent 1739314aef
commit 191daf8218
14 changed files with 139 additions and 44 deletions

View File

@@ -1,14 +1,16 @@
using System.Threading.Tasks; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using SeniorAssistant.Models;
using SeniorAssistant.Controllers;
using LinqToDB;
using System.Linq;
namespace IdentityDemo.Controllers namespace IdentityDemo.Controllers
{ {
[ApiExplorerSettings(IgnoreApi = true)] [ApiExplorerSettings(IgnoreApi = true)]
[Route("[controller]/[action]")] [Route("[controller]/[action]")]
public class AccountController : Controller public class AccountController : BaseController
{ {
/* /*
private readonly UserManager<User> _userManager; private readonly UserManager<User> _userManager;
@@ -41,19 +43,24 @@ namespace IdentityDemo.Controllers
*/ */
[HttpPost] [HttpPost]
public ActionResult _login(string username, string password, bool rememberMe) public ActionResult _login(string username, string password)
{ {
var result = username != null && password != null && username.Equals("acc1") && password.Equals("123"); //await _signInManager.PasswordSignInAsync(userName, password, rememberMe, lockoutOnFailure: false);
JsonResponse response = new JsonResponse(); JsonResponse response = new JsonResponse();
response.Success = false; response.Success = false;
response.Message = "Username or password is invalid."; response.Message = "Username or password is invalid.";
if (result) var strunz = Db.GetTable<User>().Where(user => user.Username.Equals(username) && user.Password.Equals(password)).ToListAsync().Result;
if (strunz.Count == 1)
{ {
var loggedUser = HttpContext.Session.GetString("username"); var loggedUser = HttpContext.Session.GetString("username");
if (loggedUser==null || !loggedUser.Equals(username)) if (loggedUser==null || !loggedUser.Equals(username))
{ {
HttpContext.Session.SetString("username", username); HttpContext.Session.SetString("username", username);
HttpContext.Session.SetString("email", strunz.First().Email);
HttpContext.Session.SetString("name", strunz.First().Name);
HttpContext.Session.SetString("isdoc", strunz.First().Doctor?"true":"false");
//HttpContext.Session.SetString("lastname", strunz.First().LastName);
response.Success = true; response.Success = true;
response.Message = ""; response.Message = "";
} }
@@ -65,15 +72,31 @@ namespace IdentityDemo.Controllers
return Json(response); return Json(response);
} }
[HttpPost]
public ActionResult _logout() public ActionResult _logout()
{ {
HttpContext.Session.Clear(); HttpContext.Session.Clear();
return Json(new JsonResponse()); return Json(new JsonResponse());
} }
public ActionResult _register() [HttpPost]
public ActionResult _register(Register register)
{ {
return Json(new JsonResponse()); if(ModelState.IsValid)
{
User user = new User() { Username = register.Username, Email = register.Email, Password = register.Password};
try
{
Db.Insert(user);
}
catch
{
return Json(new JsonResponse() { Success = false, Message = "Username already exist" });
}
_login(user.Username, user.Password);
return Json(new JsonResponse() { Success = true });
}
return Json(new JsonResponse() { Success = false, Message = "Modello non valido" });
} }
internal class JsonResponse internal class JsonResponse
{ {

View File

@@ -7,15 +7,13 @@ namespace SeniorAssistant.Models
{ {
[PrimaryKey] [PrimaryKey]
[NotNull] [NotNull]
[Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)]
public string Username { get; set; } public string Username { get; set; }
[PrimaryKey] [PrimaryKey]
[NotNull] [NotNull]
public DateTime Time { get; set; } public DateTime Time { get; set; }
[Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)]
public User user { get; set; }
public double Value { get; set; } public double Value { get; set; }
} }
} }

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SeniorAssistant.Models
{
public class Register
{
public string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public bool Doctor { get; set; }
}
}

View File

@@ -7,15 +7,13 @@ namespace SeniorAssistant.Models
{ {
[PrimaryKey] [PrimaryKey]
[NotNull] [NotNull]
[Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)]
public string Username { get; set; } public string Username { get; set; }
[PrimaryKey] [PrimaryKey]
[NotNull] [NotNull]
public DateTime Time { get; set; } public DateTime Time { get; set; }
[Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)]
public User user { get; set; }
public long Value { get; set; } public long Value { get; set; }
} }
} }

View File

@@ -7,15 +7,13 @@ namespace SeniorAssistant.Models
{ {
[PrimaryKey] [PrimaryKey]
[NotNull] [NotNull]
[Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)]
public string Username { get; set; } public string Username { get; set; }
[PrimaryKey] [PrimaryKey]
[NotNull] [NotNull]
public DateTime Time { get; set; } public DateTime Time { get; set; }
[Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)]
public User user { get; set; }
public long Value { get; set; } public long Value { get; set; }
} }
} }

View File

@@ -10,6 +10,17 @@ namespace SeniorAssistant.Models
public string Username { get; set; } public string Username { get; set; }
[NotNull] [NotNull]
public string Email { get; set; }
[NotNull]
public string Password { get; set; }
[NotNull]
public bool Doctor { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string LastName { get; set; }
} }
} }

View File

@@ -132,7 +132,8 @@ namespace SeniorAssistant
int count = 0; int count = 0;
foreach (string user in users) foreach (string user in users)
{ {
db.InsertOrReplace(new User { Name = user, Username = baseUsername + (count == 0 ? "" : "" + count) }); var username = baseUsername + count;
db.InsertOrReplace(new User { Name = user, Username = username, Password = username, Email = username + "@email.st" } );
count++; count++;
} }
} }

View File

@@ -5,6 +5,26 @@ logo sito
disattivare l-aside e le opzioni disattivare l-aside e le opzioni
se non loggato deve tornare qua se non loggato deve tornare qua
--> -->
<h1> @inject IHttpContextAccessor HttpContextAccessor
ciao noob
</h1> @{
string session = HttpContextAccessor.HttpContext.Session.GetString("username");
}
<div class="content">
@if (session == null)
{
<div class="login-box">
@{ await Html.RenderPartialAsync("Login"); }
</div>
<div class="login-box">
@{ await Html.RenderPartialAsync("Register");
}
</div>
}
else
{
await Html.RenderPartialAsync("Profile");
}
</div>

View File

@@ -1,9 +1,8 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">
<!-- The user image in the navbar--> <!-- The user image in the navbar-->
<!-- hidden-xs hides the username on small devices so only the image appears. --> <!-- hidden-xs hides the username on small devices so only the image appears. -->
<span id="user-name" class="hidden-xs">Login</span>
</a> </a>
<ul class="dropdown-menu"> <ul style="list-style-type:none">
<li class="user-header"> <li class="user-header">
<input type="text" id="username" placeholder="username" /> <input type="text" id="username" placeholder="username" />
<input type="password" id="password" placeholder="password" /> <input type="password" id="password" placeholder="password" />

View File

@@ -107,7 +107,10 @@
<!-- User Account Menu --> <!-- User Account Menu -->
<li id="user-menu" class="dropdown user user-menu"> <li id="user-menu" class="dropdown user user-menu">
<!-- Menu Toggle Button --> <!-- Menu Toggle Button -->
@{ await Html.RenderPartialAsync(session == null ? "Login" : "Logout", session); } @if(session != null)
{
await Html.RenderPartialAsync("Logout", session);
}
</li> </li>
<!-- Control Sidebar Toggle Button --> <!-- Control Sidebar Toggle Button -->

View File

@@ -0,0 +1,16 @@
@model User
@inject IHttpContextAccessor HttpContextAccessor
@{
var session = HttpContextAccessor.HttpContext.Session;
}
<div class="content">
<h2 class="alert-success" style="text-align:center">
Welcome @session.GetString("username")
</h2>
name: @session.GetString("name")<br />
lastname: @session.GetString("lastname")<br />
email: @session.GetString("email")
</div>

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace SeniorAssistant.Views.Shared
{
public class SuccessModel : PageModel
{
public void OnGet()
{
}
}
}

View File

@@ -1,40 +1,37 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> <ul style="list-style: none">
<!-- The user image in the navbar-->
<!-- hidden-xs hides the username on small devices so only the image appears. -->
<span id="user-name" class="hidden-xs">Register</span>
</a>
<ul class="dropdown-menu">
<li class="user-header"> <li class="user-header">
<input type="email" id="mail" placeholder="E-mail"/> <input type="text" id="regUsername" placeholder="username" />
<input type="text" id="username" placeholder="username" /> <input type="password" id="regPassword" placeholder="password" />
<input type="password" id="password" placeholder="password" /> <input type="email" id="regEmail" placeholder="example@qualcosa.qualcosa" />
<label>Doc?</label><input type="checkbox" id="regDoctor" />
<div> <div>
<button class="btn-default btn btn-flat" id="register-btn">Register</button> <button class="btn-default btn btn-flat" id="register-btn">Register</button>
</div> </div>
<p id="msg" class="login-box-msg"></p> <p id="msg-reg" class="login-box-msg"></p>
</li> </li>
</ul> </ul>
<script> <script>
$("#register-btn").on("click", function () { $("#register-btn").on("click", function () {
var userName = $("#username").val(); var regUsername = $("#regUsername").val();
var password = $("#password").val(); var regPassword = $("#regPassword").val();
var mail = $("#mail").val(); var regEmail = $("#regEmail").val();
var regDoctor = $("#regDoctor").is(":checked");
$.ajax({ $.ajax({
url: "/Account/_register", url: "/Account/_register",
data: { UserName: userName, Password: password, Email: mail}, data: { Username: regUsername, Password: regPassword, Email: regEmail},
dataType: "json", dataType: "json",
type: "POST", type: "POST",
success: function (data) { success: function (data) {
console.log(data); //se data.success->reload
var msg = $("#msg"); //se data.fail->indica errori
if (data.success) {
msg.hide();
console.log(data);
var msg = $("#msg-reg");
if (data.success) {
window.location.reload();
} else { } else {
msg.html(data.message).show(); msg.html(data.message).show();
$("#user-menu").addClass("open");
} }
}, },
error: function (xhr, status, error) { error: function (xhr, status, error) {

Binary file not shown.