Migliorato login/logout
Ma forse si passa alla versione con modelli per login e register in futuro
This commit is contained in:
@@ -71,137 +71,14 @@ namespace IdentityDemo.Controllers
|
|||||||
return Json(new JsonResponse());
|
return Json(new JsonResponse());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ActionResult _register()
|
||||||
|
{
|
||||||
|
return Json(new JsonResponse());
|
||||||
|
}
|
||||||
internal class JsonResponse
|
internal class JsonResponse
|
||||||
{
|
{
|
||||||
public bool Success { get; internal set; }
|
public bool Success { get; internal set; }
|
||||||
public string Message { get; internal set; }
|
public string Message { get; internal set; }
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
[HttpPost]
|
|
||||||
[AllowAnonymous]
|
|
||||||
[ValidateAntiForgeryToken]
|
|
||||||
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
|
|
||||||
{
|
|
||||||
ViewData["ReturnUrl"] = returnUrl;
|
|
||||||
if (ModelState.IsValid)
|
|
||||||
{
|
|
||||||
// This doesn't count login failures towards account lockout
|
|
||||||
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
|
|
||||||
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
|
|
||||||
if (result.Succeeded)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("User logged in.");
|
|
||||||
return RedirectToLocal(returnUrl);
|
|
||||||
}
|
|
||||||
if (result.IsLockedOut)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("User account locked out.");
|
|
||||||
return RedirectToAction(nameof(Lockout));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
|
|
||||||
return View(model);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we got this far, something failed, redisplay form
|
|
||||||
return View(model);
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
|
||||||
[AllowAnonymous]
|
|
||||||
public IActionResult Lockout()
|
|
||||||
{
|
|
||||||
return View();
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
|
||||||
[AllowAnonymous]
|
|
||||||
public IActionResult Register(string returnUrl = null)
|
|
||||||
{
|
|
||||||
ViewData["ReturnUrl"] = returnUrl;
|
|
||||||
return View();
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost]
|
|
||||||
[AllowAnonymous]
|
|
||||||
[ValidateAntiForgeryToken]
|
|
||||||
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
|
|
||||||
{
|
|
||||||
ViewData["ReturnUrl"] = returnUrl;
|
|
||||||
if (ModelState.IsValid)
|
|
||||||
{
|
|
||||||
var user = new User { UserName = model.Email, Email = model.Email };
|
|
||||||
var result = await _userManager.CreateAsync(user, model.Password);
|
|
||||||
if (result.Succeeded)
|
|
||||||
{
|
|
||||||
await _signInManager.SignInAsync(user, isPersistent: false);
|
|
||||||
_logger.LogInformation("User created a new account with password.");
|
|
||||||
return RedirectToLocal(returnUrl);
|
|
||||||
}
|
|
||||||
AddErrors(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we got this far, something failed, redisplay form
|
|
||||||
return View(model);
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost]
|
|
||||||
[ValidateAntiForgeryToken]
|
|
||||||
public async Task<IActionResult> Logout()
|
|
||||||
{
|
|
||||||
await _signInManager.SignOutAsync();
|
|
||||||
_logger.LogInformation("User logged out.");
|
|
||||||
return RedirectToAction(nameof(HomeController.Index), "Home");
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
|
||||||
[AllowAnonymous]
|
|
||||||
public async Task<IActionResult> ConfirmEmail(string userId, string code)
|
|
||||||
{
|
|
||||||
if (userId == null || code == null)
|
|
||||||
{
|
|
||||||
return RedirectToAction(nameof(HomeController.Index), "Home");
|
|
||||||
}
|
|
||||||
var user = await _userManager.FindByIdAsync(userId);
|
|
||||||
if (user == null)
|
|
||||||
{
|
|
||||||
throw new ApplicationException($"Unable to load user with ID '{userId}'.");
|
|
||||||
}
|
|
||||||
var result = await _userManager.ConfirmEmailAsync(user, code);
|
|
||||||
return View(result.Succeeded ? "ConfirmEmail" : "Error");
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
|
||||||
public IActionResult AccessDenied()
|
|
||||||
{
|
|
||||||
return View();
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Helpers
|
|
||||||
|
|
||||||
private void AddErrors(IdentityResult result)
|
|
||||||
{
|
|
||||||
foreach (var error in result.Errors)
|
|
||||||
{
|
|
||||||
ModelState.AddModelError(string.Empty, error.Description);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private IActionResult RedirectToLocal(string returnUrl)
|
|
||||||
{
|
|
||||||
if (Url.IsLocalUrl(returnUrl))
|
|
||||||
{
|
|
||||||
return Redirect(returnUrl);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return RedirectToAction(nameof(HomeController.Index), "Home");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,10 +38,16 @@ namespace SeniorAssistant.Controllers
|
|||||||
return View();
|
return View();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Route("Users")]
|
||||||
|
public IActionResult Users()
|
||||||
|
{
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
|
||||||
[Route("User/{User}")]
|
[Route("User/{User}")]
|
||||||
public IActionResult SingleUser(string user)
|
public IActionResult SingleUser(string user)
|
||||||
{
|
{
|
||||||
return View("user", user);
|
return View("data", user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,53 +1,10 @@
|
|||||||
@model IEnumerable<User>
|
<!--
|
||||||
@{
|
pg di registering
|
||||||
ViewBag.Title = "Hello Razor";
|
se gia loggato reindirizza al profilo(ancora da fare)
|
||||||
}
|
logo sito
|
||||||
|
disattivare l-aside e le opzioni
|
||||||
<div id="grid"></div>
|
se non loggato deve tornare qua
|
||||||
|
-->
|
||||||
<script>
|
<h1>
|
||||||
$(document).ready(function () {
|
ciao noob
|
||||||
var baseUrl = "@Url.Content("~/api/user/")";
|
</h1>
|
||||||
|
|
||||||
$("#grid").kendoGrid({
|
|
||||||
dataSource: {
|
|
||||||
transport: {
|
|
||||||
read: { url: baseUrl, type: "GET" }
|
|
||||||
|
|
||||||
/*
|
|
||||||
parameterMap: function (model, operation) {
|
|
||||||
if (operation !== "read" && model) {
|
|
||||||
return kendo.stringify(model);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
},
|
|
||||||
serverPaging: false,
|
|
||||||
serverSorting: false,
|
|
||||||
batch: false,
|
|
||||||
schema: {
|
|
||||||
model: {
|
|
||||||
id: "username",
|
|
||||||
fields: {
|
|
||||||
username: { type: "string" },
|
|
||||||
name: { type: "string" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
scrollable: true,
|
|
||||||
sortable: true,
|
|
||||||
filterable: true,
|
|
||||||
editable: false,
|
|
||||||
columns: [
|
|
||||||
{ field: "username", title: "Username" },
|
|
||||||
{ field: "name", title: "Name" },
|
|
||||||
{ field: "url", title: "",template:'<a href=/user/#=username#>Vedi Dati</a>'}/*,
|
|
||||||
{ field: "time", title: "Date/Time", format: "{dd/MM/yyyy HH}" },
|
|
||||||
{ field: "value", title: "Heartbeats" }
|
|
||||||
*/
|
|
||||||
]
|
|
||||||
});
|
|
||||||
})
|
|
||||||
|
|
||||||
</script>
|
|
||||||
53
SeniorAssistant/Views/Home/Users.cshtml
Normal file
53
SeniorAssistant/Views/Home/Users.cshtml
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
@model IEnumerable<User>
|
||||||
|
@{
|
||||||
|
ViewBag.Title = "Hello Razor";
|
||||||
|
}
|
||||||
|
|
||||||
|
<div id="grid"></div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
$(document).ready(function () {
|
||||||
|
var baseUrl = "@Url.Content("~/api/user/")";
|
||||||
|
|
||||||
|
$("#grid").kendoGrid({
|
||||||
|
dataSource: {
|
||||||
|
transport: {
|
||||||
|
read: { url: baseUrl, type: "GET" }
|
||||||
|
|
||||||
|
/*
|
||||||
|
parameterMap: function (model, operation) {
|
||||||
|
if (operation !== "read" && model) {
|
||||||
|
return kendo.stringify(model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
},
|
||||||
|
serverPaging: false,
|
||||||
|
serverSorting: false,
|
||||||
|
batch: false,
|
||||||
|
schema: {
|
||||||
|
model: {
|
||||||
|
id: "username",
|
||||||
|
fields: {
|
||||||
|
username: { type: "string" },
|
||||||
|
name: { type: "string" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
scrollable: true,
|
||||||
|
sortable: true,
|
||||||
|
filterable: true,
|
||||||
|
editable: false,
|
||||||
|
columns: [
|
||||||
|
{ field: "username", title: "Username" },
|
||||||
|
{ field: "name", title: "Name" },
|
||||||
|
{ field: "url", title: "",template:'<a href=/user/#=username#>Vedi Dati</a>'}/*,
|
||||||
|
{ field: "time", title: "Date/Time", format: "{dd/MM/yyyy HH}" },
|
||||||
|
{ field: "value", title: "Heartbeats" }
|
||||||
|
*/
|
||||||
|
]
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
</script>
|
||||||
@@ -1,9 +1,18 @@
|
|||||||
<div class="">
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
|
||||||
<input type="text" id="username" placeholder="username" />
|
<!-- The user image in the navbar-->
|
||||||
<input type="password" id="password" placeholder="password" />
|
<!-- hidden-xs hides the username on small devices so only the image appears. -->
|
||||||
<button class="btn-default btn btn-flat" id="login-btn">Login</button>
|
<span id="user-name" class="hidden-xs">Login</span>
|
||||||
<p id="msg" class="login-box-msg"></p>
|
</a>
|
||||||
</div>
|
<ul class="dropdown-menu">
|
||||||
|
<li class="user-header">
|
||||||
|
<input type="text" id="username" placeholder="username" />
|
||||||
|
<input type="password" id="password" placeholder="password" />
|
||||||
|
<div>
|
||||||
|
<button class="btn-default btn btn-flat" id="login-btn">Login</button>
|
||||||
|
</div>
|
||||||
|
<p id="msg" class="login-box-msg"></p>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
$("#login-btn").on("click", function () {
|
$("#login-btn").on("click", function () {
|
||||||
|
|||||||
@@ -1,6 +1,44 @@
|
|||||||
<div class="">
|
@model string
|
||||||
<button class="btn-default btn btn-flat" id="logout-btn">Logout</button>
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
|
||||||
</div>
|
<!-- The user image in the navbar-->
|
||||||
|
<img src="~/AdminLTE-2.4.3/dist/img/user2-160x160.jpg" class="user-image" alt="User Image">
|
||||||
|
<!-- hidden-xs hides the username on small devices so only the image appears. -->
|
||||||
|
<span id="user-name" class="hidden-xs">@Model</span>
|
||||||
|
</a>
|
||||||
|
<ul class="dropdown-menu">
|
||||||
|
<!-- The user image in the menu -->
|
||||||
|
<li class="user-header">
|
||||||
|
<img src="~/AdminLTE-2.4.3/dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">
|
||||||
|
<p>
|
||||||
|
Alexander Pierce - Web Developer
|
||||||
|
<small>Member since Nov. 2012</small>
|
||||||
|
</p>
|
||||||
|
</li>
|
||||||
|
<!-- Menu Body -->
|
||||||
|
<li class="user-body">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-xs-4 text-center">
|
||||||
|
<a href="#">Followers</a>
|
||||||
|
</div>
|
||||||
|
<div class="col-xs-4 text-center">
|
||||||
|
<a href="#">Sales</a>
|
||||||
|
</div>
|
||||||
|
<div class="col-xs-4 text-center">
|
||||||
|
<a href="#">Friends</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<!-- /.row -->
|
||||||
|
</li>
|
||||||
|
<!-- Menu Footer-->
|
||||||
|
<li class="user-footer">
|
||||||
|
<div class="pull-left">
|
||||||
|
<a href="#" class="btn btn-default btn-flat">Profile</a>
|
||||||
|
</div>
|
||||||
|
<div class="pull-right">
|
||||||
|
<a href="#" id="logout-btn" class="btn btn-default btn-flat">Logout</a>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
$("#logout-btn").on("click", function () {
|
$("#logout-btn").on("click", function () {
|
||||||
@@ -9,7 +47,7 @@
|
|||||||
dataType: "json",
|
dataType: "json",
|
||||||
type: "POST",
|
type: "POST",
|
||||||
success: function () {
|
success: function () {
|
||||||
window.location.reload();
|
window.location.href = "/";
|
||||||
},
|
},
|
||||||
error: function (xhr, status, error) {
|
error: function (xhr, status, error) {
|
||||||
alert(xhr.responseText)
|
alert(xhr.responseText)
|
||||||
|
|||||||
@@ -107,50 +107,8 @@
|
|||||||
<!-- 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); }
|
||||||
|
|
||||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
|
|
||||||
<!-- The user image in the navbar-->
|
|
||||||
<img src="~/AdminLTE-2.4.3/dist/img/user2-160x160.jpg" class="user-image" alt="User Image">
|
|
||||||
<!-- hidden-xs hides the username on small devices so only the image appears. -->
|
|
||||||
<span id="user-name" class="hidden-xs">@session</span>
|
|
||||||
</a>
|
|
||||||
<ul class="dropdown-menu">
|
|
||||||
<!-- The user image in the menu -->
|
|
||||||
<li class="user-body">
|
|
||||||
@{ await Html.RenderPartialAsync(session == null?"Login":"Logout"); }
|
|
||||||
</li>
|
|
||||||
<li class="user-header">
|
|
||||||
<img src="~/AdminLTE-2.4.3/dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">
|
|
||||||
<p>
|
|
||||||
Alexander Pierce - Web Developer
|
|
||||||
<small>Member since Nov. 2012</small>
|
|
||||||
</p>
|
|
||||||
</li>
|
|
||||||
<!-- Menu Body -->
|
|
||||||
<li class="user-body">
|
|
||||||
<div class="row">
|
|
||||||
<div class="col-xs-4 text-center">
|
|
||||||
<a href="#">Followers</a>
|
|
||||||
</div>
|
|
||||||
<div class="col-xs-4 text-center">
|
|
||||||
<a href="#">Sales</a>
|
|
||||||
</div>
|
|
||||||
<div class="col-xs-4 text-center">
|
|
||||||
<a href="#">Friends</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<!-- /.row -->
|
|
||||||
</li>
|
|
||||||
<!-- Menu Footer-->
|
|
||||||
<li class="user-footer">
|
|
||||||
<div class="pull-left">
|
|
||||||
<a href="#" class="btn btn-default btn-flat">Profile</a>
|
|
||||||
</div>
|
|
||||||
<div class="pull-right">
|
|
||||||
<a href="#" class="btn btn-default btn-flat">Sign out</a>
|
|
||||||
</div>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
</li>
|
</li>
|
||||||
<!-- Control Sidebar Toggle Button -->
|
<!-- Control Sidebar Toggle Button -->
|
||||||
<li>
|
<li>
|
||||||
|
|||||||
45
SeniorAssistant/Views/Shared/Register.cshtml
Normal file
45
SeniorAssistant/Views/Shared/Register.cshtml
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<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">
|
||||||
|
<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" />
|
||||||
|
<div>
|
||||||
|
<button class="btn-default btn btn-flat" id="register-btn">Register</button>
|
||||||
|
</div>
|
||||||
|
<p id="msg" 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();
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: "/Account/_register",
|
||||||
|
data: { UserName: userName, Password: password, Email: mail},
|
||||||
|
dataType: "json",
|
||||||
|
type: "POST",
|
||||||
|
success: function (data) {
|
||||||
|
console.log(data);
|
||||||
|
var msg = $("#msg");
|
||||||
|
if (data.success) {
|
||||||
|
msg.hide();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
msg.html(data.message).show();
|
||||||
|
$("#user-menu").addClass("open");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function (xhr, status, error) {
|
||||||
|
alert(xhr.responseText)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
</script>
|
||||||
16
SeniorAssistant/Views/Shared/Register.cshtml.cs
Normal file
16
SeniorAssistant/Views/Shared/Register.cshtml.cs
Normal 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 RegisterModel : PageModel
|
||||||
|
{
|
||||||
|
public void OnGet()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
Reference in New Issue
Block a user