-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring in progress and migration to new project system
- Loading branch information
1 parent
07b3c0b
commit 52f0a0d
Showing
69 changed files
with
1,154 additions
and
60,530 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using GenericMvc.Models.ViewModels; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace GenericMvc.Auth.Controllers | ||
{ | ||
/// <summary> | ||
/// In terms of registering users, this is merely designed for registering non-privileged users | ||
/// In general, the use of this class is designed for non-privileged Users or privileged user whom already registered via the account controller class | ||
/// | ||
/// </summary> | ||
/// <typeparam name="TKey">The type of the key.</typeparam> | ||
/// <typeparam name="TUser">The type of the user.</typeparam> | ||
/// <seealso cref="Microsoft.AspNetCore.Mvc.Controller" /> | ||
/// <seealso cref="GenericMvcModels.Controllers.IAuthApiController" /> | ||
[Route("/api/[controller]/[action]/"), Authorize] | ||
public class AuthApi<TKey, TUser, TRegisterViewModel, TLoginViewModel> : Controller, IAuthApi<TRegisterViewModel, TLoginViewModel> | ||
where TLoginViewModel : LoginViewModel | ||
where TRegisterViewModel : RegisterViewModel | ||
where TKey : IEquatable<TKey> | ||
where TUser : Models.User, new() | ||
{ | ||
private readonly SignInManager<TUser> _signInManager; | ||
|
||
private readonly UserManager<TUser> _userManager; | ||
|
||
private readonly ILogger<TUser> _logger; | ||
|
||
public AuthApi(SignInManager<TUser> signInManager, UserManager<TUser> userManager, ILogger<TUser> logger) | ||
{ | ||
_signInManager = signInManager ?? throw new ArgumentNullException(nameof(signInManager)); | ||
|
||
_userManager = userManager ?? throw new ArgumentNullException(nameof(userManager)); | ||
|
||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
[HttpPost, AllowAnonymous] | ||
public async Task<IActionResult> Login([FromBody] TLoginViewModel viewModel) | ||
{ | ||
if (viewModel == null || !ModelState.IsValid) | ||
return BadRequest(); | ||
|
||
// 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(viewModel.UserName, viewModel.Password, viewModel.IsPersistent, lockoutOnFailure: false); | ||
|
||
if (result.Succeeded) | ||
return Json(result); | ||
|
||
//no way to two factor bots at the moment | ||
if (result.RequiresTwoFactor) | ||
return Unauthorized(); | ||
|
||
if (result.IsLockedOut) | ||
return new StatusCodeResult(403); | ||
|
||
return Unauthorized(); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Logout() | ||
{ | ||
await _signInManager.SignOutAsync(); | ||
|
||
return Ok(); | ||
} | ||
|
||
[HttpPost, AllowAnonymous] | ||
public async Task<IActionResult> Register([FromBody] TRegisterViewModel viewModel) | ||
{ | ||
if (viewModel == null && !ModelState.IsValid) | ||
return BadRequest(ModelState); | ||
|
||
//todo make this | ||
var user = new TUser() | ||
{ | ||
UserName = viewModel.UserName | ||
}; | ||
|
||
user.FromRegisterViewModel(viewModel); | ||
|
||
//if (model.Password != model.ConfirmPassword) | ||
//return BadRequest("Supplied Passwords are not the same"); | ||
|
||
var result = await _userManager.CreateAsync(user, viewModel.Password); | ||
|
||
if (!result.Succeeded) | ||
return new StatusCodeResult(500); | ||
|
||
await _signInManager.SignInAsync(user, viewModel.IsPersistent); | ||
|
||
_logger.LogInformation($"{user.UserName} create a new account with password."); | ||
|
||
return Json(result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using GenericMvc.Models.ViewModels; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace GenericMvc.Auth.Controllers | ||
{ | ||
public interface IAuthApi<TRegisterViewModel, TLoginViewModel> | ||
where TLoginViewModel : LoginViewModel | ||
where TRegisterViewModel : RegisterViewModel | ||
{ | ||
Task<IActionResult> Register([FromBody] TRegisterViewModel viewModel); | ||
|
||
Task<IActionResult> Login([FromBody] TLoginViewModel viewModel); | ||
|
||
Task<IActionResult> Logout(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netstandard1.6</TargetFramework> | ||
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion> | ||
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netstandard1.6' ">$(PackageTargetFallback);portable-net451+win8</PackageTargetFallback> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="1.1.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="1.1.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\GenericMvc.Models\GenericMvc.Models.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using GenericMvc.Models.ViewModels; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace GenericMvc.Auth.Models | ||
{ | ||
public interface IUser | ||
{ | ||
string UserName { get; } | ||
|
||
void FromRegisterViewModel<TRegisterViewModel>(TRegisterViewModel viewModel) where TRegisterViewModel : RegisterViewModel; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using GenericMvc.Models.ViewModels; | ||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | ||
using System; | ||
|
||
namespace GenericMvc.Auth.Models | ||
{ | ||
/// <summary> | ||
/// Base abstract class to represent non-privileged users | ||
/// These users have no roles or claims external to their identity | ||
/// They should only be allowed to edit data that lives under their data object | ||
/// They should only be allowed to view data that is not access controlled | ||
/// </summary> | ||
/// <seealso cref="Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUser" /> | ||
public abstract class User : IdentityUser, IUser | ||
{ | ||
public User(): base() | ||
{ | ||
|
||
} | ||
|
||
public abstract void FromRegisterViewModel<TRegisterViewModel>(TRegisterViewModel viewModel) where TRegisterViewModel : RegisterViewModel; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netstandard1.4</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" /> | ||
<ProjectReference Include="..\GenericMvc.Models\GenericMvc.Models.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netstandard1.6</TargetFramework> | ||
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion> | ||
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netstandard1.6' ">$(PackageTargetFallback);portable-net451+win8</PackageTargetFallback> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="System.Reactive" Version="3.1.1" /> | ||
<PackageReference Include="System.Reactive.Linq" Version="3.1.1" /> | ||
<PackageReference Include="Mime-Detective" Version="0.0.2" /> | ||
<ProjectReference Include="..\GenericMvc.Models\GenericMvc.Models.csproj" /> | ||
<ProjectReference Include="..\src\GenericMvc.Data\GenericMvc.Data.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.