Converted Login and Register methods to razor pages.

pull/179/head
Halil İbrahim Kalkan 8 years ago
parent 0f70f64134
commit ffdf1958f1

@ -27,6 +27,14 @@ namespace Volo.Abp.Account.Web
)
);
options.FileSets.Add(
new EmbeddedFileSet(
"/Pages/",
GetType().GetTypeInfo().Assembly,
"Volo.Abp.Account.Web.Pages"
)
);
options.FileSets.Add(
new EmbeddedFileSet(
"/",

@ -1,49 +0,0 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Account.Web.Areas.Account.Models.Login;
using Volo.Abp.Identity;
using Volo.Abp.Ui;
namespace Volo.Abp.Account.Web.Areas.Account.Controllers
{
[Area("Account")]
public class LoginController : AccountControllerBase
{
private readonly SignInManager<IdentityUser> _signInManager;
public LoginController(SignInManager<IdentityUser> signInManager)
{
_signInManager = signInManager;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Index(LoginModel loginModel, string returnUrl = "", string returnUrlHash = "")
{
if (!ModelState.IsValid)
{
throw new NotImplementedException();
}
var result = await _signInManager.PasswordSignInAsync(
loginModel.UserNameOrEmailAddress,
loginModel.Password,
loginModel.RememberMe,
true
);
if (!result.Succeeded)
{
throw new UserFriendlyException("Login failed!"); //TODO: Handle other cases, do not throw exception
}
return RedirectSafely(returnUrl, returnUrlHash);
}
}
}

@ -1,15 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Identity;
namespace Volo.Abp.Account.Web.Areas.Account.Controllers
{
[Area("Account")]
public class LogoutController : AccountControllerBase
public class LogoutController : AbpController
{
private readonly SignInManager<IdentityUser> _signInManager;
@ -22,7 +20,7 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers
{
await _signInManager.SignOutAsync();
return RedirectToAction("Index", "Login");
return RedirectToPage("/Account/Login");
}
}
}

@ -1,52 +0,0 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Account.Web.Areas.Account.Models.Register;
using Volo.Abp.Identity;
namespace Volo.Abp.Account.Web.Areas.Account.Controllers
{
[Area("Account")]
public class RegisterController : AccountControllerBase
{
private readonly IdentityUserManager _userManager;
private readonly SignInManager<IdentityUser> _signInManager;
public RegisterController(IdentityUserManager userManager, SignInManager<IdentityUser> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
//TODO: [ValidateAntiForgeryToken]
public async Task<IActionResult> Index(RegisterModel registerModel, string returnUrl = "", string returnUrlHash = "")
{
if (!ModelState.IsValid)
{
throw new NotImplementedException();
}
var user = new IdentityUser(GuidGenerator.Create(), registerModel.UserName);
var result = await _userManager.CreateAsync(user, registerModel.Password);
if (!result.Succeeded)
{
throw new NotImplementedException();
}
await _userManager.SetEmailAsync(user, registerModel.EmailAddress);
await _signInManager.SignInAsync(user, isPersistent: false);
return RedirectSafely(returnUrl, returnUrlHash);
}
}
}

@ -1,17 +0,0 @@
using System.ComponentModel.DataAnnotations;
namespace Volo.Abp.Account.Web.Areas.Account.Models.Login
{
public class LoginModel
{
[Required]
[MaxLength(255)]
public string UserNameOrEmailAddress { get; set; }
[Required]
[MaxLength(32)]
public string Password { get; set; }
public bool RememberMe { get; set; }
}
}

@ -1,20 +0,0 @@
using System.ComponentModel.DataAnnotations;
namespace Volo.Abp.Account.Web.Areas.Account.Models.Register
{
public class RegisterModel
{
[Required]
[MaxLength(32)]
public string UserName { get; set; }
[Required]
[EmailAddress]
[MaxLength(255)]
public string EmailAddress { get; set; }
[Required]
[MaxLength(32)]
public string Password { get; set; }
}
}

@ -3,12 +3,12 @@ using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.RazorPages;
using Volo.Abp.Ui;
namespace Volo.Abp.Account.Web.Areas.Account.Controllers
namespace Volo.Abp.Account.Web.Pages.Account
{
public abstract class AccountControllerBase : AbpController
public abstract class AccountModelBase : AbpPageModel
{
protected RedirectResult RedirectSafely(string returnUrl, string returnUrlHash = null)
{

@ -1,6 +1,8 @@
<div class="row">
@page
@model Volo.Abp.Account.Web.Pages.Account.LoginModel
<div class="row">
<div class="col-md-3">
<form asp-action="Index" method="post">
<form method="post">
<div class="form-group">
<label for="UsernameOrEmailAddress">Username or email address</label>
<input type="text" class="form-control" id="UsernameOrEmailAddress" name="UsernameOrEmailAddress">
@ -17,9 +19,9 @@
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
<div>
<a href="@Url.Action("Index", "Register", new { returnUrl = Context.Request.Query["returnUrl"], returnUrlHash = Context.Request.Query["returnUrlHash"] })">Register</a>
<a href="@Url.Page("./Register", new { returnUrl = Model.ReturnUrl, returnUrlHash = Model.ReturnUrlHash })">Register</a>
</div>
</div>
</div>

@ -0,0 +1,72 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Identity;
using Volo.Abp.Ui;
using Volo.Abp.Uow;
namespace Volo.Abp.Account.Web.Pages.Account
{
public class LoginModel : AccountModelBase
{
public string ReturnUrl { get; set; } //TODO: Try to automatically bind from querystring!
public string ReturnUrlHash { get; set; } //TODO: Try to automatically bind from querystring!
private readonly SignInManager<IdentityUser> _signInManager;
public LoginModel(SignInManager<IdentityUser> signInManager)
{
_signInManager = signInManager;
}
public void OnGet(string returnUrl = "", string returnUrlHash = "")
{
ReturnUrl = returnUrl;
ReturnUrlHash = returnUrl;
}
//TODO: Bind input to a property instead of getting as parameter..?
[UnitOfWork] //TODO: Will be removed when we implement action filter
public virtual async Task<IActionResult> OnPostAsync(PostInput input, string returnUrl = "", string returnUrlHash = "")
{
ReturnUrl = returnUrl;
ReturnUrlHash = returnUrl;
if (!ModelState.IsValid)
{
throw new NotImplementedException();
}
var result = await _signInManager.PasswordSignInAsync(
input.UserNameOrEmailAddress,
input.Password,
input.RememberMe,
true
);
if (!result.Succeeded)
{
throw new UserFriendlyException("Login failed!"); //TODO: Handle other cases, do not throw exception
}
//TODO: Use LocalRedirect and Url.GetLocalUrl methods instead of a custom one!
return RedirectSafely(returnUrl, returnUrlHash);
}
public class PostInput
{
[Required]
[MaxLength(255)]
public string UserNameOrEmailAddress { get; set; }
[Required]
[MaxLength(32)]
public string Password { get; set; }
public bool RememberMe { get; set; }
}
}
}

@ -1,6 +1,8 @@
<div class="row">
@page
@model Volo.Abp.Account.Web.Pages.Account.RegisterModel
<div class="row">
<div class="col-md-3">
<form asp-action="Index" method="post">
<form method="post">
<div class="form-group">
<label for="UserName">Username</label>
<input type="text" class="form-control" id="UserName" name="UserName" required>

@ -0,0 +1,74 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Identity;
using Volo.Abp.Uow;
namespace Volo.Abp.Account.Web.Pages.Account
{
public class RegisterModel : AccountModelBase
{
public string ReturnUrl { get; set; } //TODO: Try to automatically bind from querystring!
public string ReturnUrlHash { get; set; } //TODO: Try to automatically bind from querystring!
private readonly IdentityUserManager _userManager;
private readonly SignInManager<IdentityUser> _signInManager;
public RegisterModel(IdentityUserManager userManager, SignInManager<IdentityUser> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
public void OnGet(string returnUrl = "", string returnUrlHash = "")
{
ReturnUrl = returnUrl;
ReturnUrlHash = returnUrl;
}
//TODO: Bind input to a property instead of getting as parameter..?
[UnitOfWork] //TODO: Will be removed when we implement action filter
public virtual async Task<IActionResult> OnPostAsync(PostInput input, string returnUrl = "", string returnUrlHash = "")
{
if (!ModelState.IsValid)
{
throw new NotImplementedException();
}
var user = new IdentityUser(GuidGenerator.Create(), input.UserName);
var result = await _userManager.CreateAsync(user, input.Password);
if (!result.Succeeded)
{
throw new NotImplementedException();
}
await _userManager.SetEmailAsync(user, input.EmailAddress);
await _signInManager.SignInAsync(user, isPersistent: false);
//TODO: Use LocalRedirect and Url.GetLocalUrl methods instead of a custom one!
return RedirectSafely(returnUrl, returnUrlHash);
}
public class PostInput
{
[Required]
[MaxLength(32)]
public string UserName { get; set; }
[Required]
[EmailAddress]
[MaxLength(255)]
public string EmailAddress { get; set; }
[Required]
[MaxLength(32)]
public string Password { get; set; }
}
}
}

@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53098/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Volo.Abp.Account.Web": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:53099/"
}
}
}

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Web">
<Import Project="..\..\common.props" />
@ -11,10 +11,11 @@
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace>Volo.Abp.Account.Web</RootNamespace>
<OutputType>Library</OutputType>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Areas\**\*.*" Exclude="bin\**;obj\**;**\*.xproj;packages\**;@(EmbeddedResource)" />
<EmbeddedResource Include="Pages\**\*.*" Exclude="*.cs" />
</ItemGroup>
<ItemGroup>

@ -23,8 +23,4 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Volo\Abp\AspNetCore\Mvc\RazorPages\NewFolder\" />
</ItemGroup>
</Project>

Loading…
Cancel
Save