+ 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.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SeniorAssistant.Models;
using SeniorAssistant.Controllers;
using LinqToDB;
using System.Linq;
namespace IdentityDemo.Controllers
{
[ApiExplorerSettings(IgnoreApi = true)]
[Route("[controller]/[action]")]
public class AccountController : Controller
public class AccountController : BaseController
{
/*
private readonly UserManager<User> _userManager;
@@ -41,19 +43,24 @@ namespace IdentityDemo.Controllers
*/
[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();
response.Success = false;
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");
if (loggedUser==null || !loggedUser.Equals(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.Message = "";
}
@@ -65,15 +72,31 @@ namespace IdentityDemo.Controllers
return Json(response);
}
[HttpPost]
public ActionResult _logout()
{
HttpContext.Session.Clear();
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
{

View File

@@ -7,14 +7,12 @@ namespace SeniorAssistant.Models
{
[PrimaryKey]
[NotNull]
[Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)]
public string Username { get; set; }
[PrimaryKey]
[NotNull]
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; }
}

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,14 +7,12 @@ namespace SeniorAssistant.Models
{
[PrimaryKey]
[NotNull]
[Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)]
public string Username { get; set; }
[PrimaryKey]
[NotNull]
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; }
}

View File

@@ -7,15 +7,13 @@ namespace SeniorAssistant.Models
{
[PrimaryKey]
[NotNull]
[Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)]
public string Username { get; set; }
[PrimaryKey]
[NotNull]
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; }
}
}

View File

@@ -10,6 +10,17 @@ namespace SeniorAssistant.Models
public string Username { get; set; }
[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 LastName { get; set; }
}
}

View File

@@ -132,7 +132,8 @@ namespace SeniorAssistant
int count = 0;
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++;
}
}

View File

@@ -5,6 +5,26 @@ logo sito
disattivare l-aside e le opzioni
se non loggato deve tornare qua
-->
<h1>
ciao noob
</h1>
@inject IHttpContextAccessor HttpContextAccessor
@{
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">
<!-- 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">Login</span>
</a>
<ul class="dropdown-menu">
<ul style="list-style-type:none">
<li class="user-header">
<input type="text" id="username" placeholder="username" />
<input type="password" id="password" placeholder="password" />

View File

@@ -107,7 +107,10 @@
<!-- User Account Menu -->
<li id="user-menu" class="dropdown user user-menu">
<!-- Menu Toggle Button -->
@{ await Html.RenderPartialAsync(session == null ? "Login" : "Logout", session); }
@if(session != null)
{
await Html.RenderPartialAsync("Logout", session);
}
</li>
<!-- 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">
<!-- 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">
<ul style="list-style: none">
<li class="user-header">
<input type="email" id="mail" placeholder="E-mail"/>
<input type="text" id="username" placeholder="username" />
<input type="password" id="password" placeholder="password" />
<input type="text" id="regUsername" placeholder="username" />
<input type="password" id="regPassword" placeholder="password" />
<input type="email" id="regEmail" placeholder="example@qualcosa.qualcosa" />
<label>Doc?</label><input type="checkbox" id="regDoctor" />
<div>
<button class="btn-default btn btn-flat" id="register-btn">Register</button>
</div>
<p id="msg" class="login-box-msg"></p>
<p id="msg-reg" class="login-box-msg"></p>
</li>
</ul>
<script>
$("#register-btn").on("click", function () {
var userName = $("#username").val();
var password = $("#password").val();
var mail = $("#mail").val();
var regUsername = $("#regUsername").val();
var regPassword = $("#regPassword").val();
var regEmail = $("#regEmail").val();
var regDoctor = $("#regDoctor").is(":checked");
$.ajax({
url: "/Account/_register",
data: { UserName: userName, Password: password, Email: mail},
data: { Username: regUsername, Password: regPassword, Email: regEmail},
dataType: "json",
type: "POST",
success: function (data) {
//se data.success->reload
//se data.fail->indica errori
console.log(data);
var msg = $("#msg");
var msg = $("#msg-reg");
if (data.success) {
msg.hide();
window.location.reload();
} else {
msg.html(data.message).show();
$("#user-menu").addClass("open");
}
},
error: function (xhr, status, error) {

Binary file not shown.