Skip to content

Commit

Permalink
Code after Prairie.Code() 2016-10-28
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejbalas committed Jan 6, 2017
1 parent 4413af1 commit abea502
Show file tree
Hide file tree
Showing 73 changed files with 23,987 additions and 1 deletion.
1 change: 0 additions & 1 deletion 2016-10-28-PrairieCode/PrairieCodeIdentity
Submodule PrairieCodeIdentity deleted from a8f757
32 changes: 32 additions & 0 deletions 2016-10-28-PrairieCodeIdentity/PrairieCodeIdentity.sln
@@ -0,0 +1,32 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4E866B10-F7A6-446D-9FC3-516462C9685B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8BEB8C67-4C37-4CCF-B1CA-A17F02C860B4}"
ProjectSection(SolutionItems) = preProject
global.json = global.json
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "PrairieCodeIdentity", "src\PrairieCodeIdentity\PrairieCodeIdentity.xproj", "{E40BC3B5-B1BF-476F-897F-739CF20ED101}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E40BC3B5-B1BF-476F-897F-739CF20ED101}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E40BC3B5-B1BF-476F-897F-739CF20ED101}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E40BC3B5-B1BF-476F-897F-739CF20ED101}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E40BC3B5-B1BF-476F-897F-739CF20ED101}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E40BC3B5-B1BF-476F-897F-739CF20ED101} = {4E866B10-F7A6-446D-9FC3-516462C9685B}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions 2016-10-28-PrairieCodeIdentity/global.json
@@ -0,0 +1,6 @@
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-preview2-003131"
}
}
@@ -0,0 +1,3 @@
{
"directory": "wwwroot/lib"
}
@@ -0,0 +1,54 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using PrairieCodeIdentity.Identity;
using SignInResult = Microsoft.AspNetCore.Identity.SignInResult;

namespace PrairieCodeIdentity.Controllers
{
public class AccountController : Controller
{
private readonly SignInManager<CustomUser> _signInManager;

public AccountController(SignInManager<CustomUser> signInManager)
{
_signInManager = signInManager;
}

public ActionResult Login()
{
return View();
}

[HttpPost]
public async Task<ActionResult> Login(LoginViewModel userLogin)
{
//Response.Cookies.Append("demo.username", userLogin.UserName);

//await _signInManager.PasswordSignInAsync(userLogin.UserName, string.Empty, false, false);

var result = await _signInManager.PasswordSignInAsync(userLogin.UserName, string.Empty, false, false);

if (result == SignInResult.Success)
{
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Invalid login attempt.");
return View(userLogin);
}

return RedirectToAction("Index", "Home");
}

public async Task<ActionResult> LogOff()
{
//Response.Cookies.Delete("demo.username");

await _signInManager.SignOutAsync();

return RedirectToAction("Index", "Home");
}
}
}
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace PrairieCodeIdentity.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}

[Authorize("CanAccessVIPArea")]
public IActionResult About()
{
ViewData["Message"] = "This is the VIP area";

return View();
}

public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";

return View();
}

public IActionResult Error()
{
return View();
}
}
}
@@ -0,0 +1,7 @@
namespace PrairieCodeIdentity.Identity
{
public class CustomRole
{

}
}
@@ -0,0 +1,64 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace PrairieCodeIdentity.Identity
{
public class CustomRoleStore : IRoleStore<CustomRole>
{
public Task<IdentityResult> CreateAsync(CustomRole role, CancellationToken cancellationToken)
{
return Task.FromResult(IdentityResult.Failed());
}

public Task<IdentityResult> DeleteAsync(CustomRole role, CancellationToken cancellationToken)
{
return Task.FromResult(IdentityResult.Failed());
}

public void Dispose()
{
}

public Task<CustomRole> FindByIdAsync(string roleId, CancellationToken cancellationToken)
{
return Task.FromResult<CustomRole>(null);
}

public Task<CustomRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken)
{
return Task.FromResult<CustomRole>(null);
}

public Task<string> GetNormalizedRoleNameAsync(CustomRole role, CancellationToken cancellationToken)
{
return Task.FromResult<string>(null);
}

public Task<string> GetRoleIdAsync(CustomRole role, CancellationToken cancellationToken)
{
return Task.FromResult<string>(null);
}

public Task<string> GetRoleNameAsync(CustomRole role, CancellationToken cancellationToken)
{
return Task.FromResult<string>(null);
}

public Task SetNormalizedRoleNameAsync(CustomRole role, string normalizedName,
CancellationToken cancellationToken)
{
return Task.FromResult(0);
}

public Task SetRoleNameAsync(CustomRole role, string roleName, CancellationToken cancellationToken)
{
return Task.FromResult(0);
}

public Task<IdentityResult> UpdateAsync(CustomRole role, CancellationToken cancellationToken)
{
return Task.FromResult(IdentityResult.Failed());
}
}
}
@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using System.Linq;
using System.Security.Claims;

namespace PrairieCodeIdentity.Identity
{
public class CustomStore : IUserStore<CustomUser>, IUserPasswordStore<CustomUser>, IUserClaimStore<CustomUser>
{
private readonly List<CustomUser> users;

public CustomStore()
{
users = new List<CustomUser>()
{
new CustomUser() {Id = 1, UserName = "ondrej"},
new CustomUser() {Id = 2, UserName = "james"}
};
}

public Task<IdentityResult> CreateAsync(CustomUser user, CancellationToken cancellationToken)
{
return Task.FromResult(IdentityResult.Failed());
}

public Task<IdentityResult> DeleteAsync(CustomUser user, CancellationToken cancellationToken)
{
return Task.FromResult(IdentityResult.Failed());
}

public void Dispose()
{
}

public Task<CustomUser> FindByIdAsync(string userId, CancellationToken cancellationToken)
{
return Task.FromResult(users.FirstOrDefault(user => user.Id.ToString() == userId));
}

public Task<CustomUser> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken)
{
return
Task.FromResult(
users.FirstOrDefault(
user =>
string.Equals(user.UserName, normalizedUserName, StringComparison.CurrentCultureIgnoreCase)));
}

public Task<string> GetNormalizedUserNameAsync(CustomUser user, CancellationToken cancellationToken)
{
return Task.FromResult(user.UserName.ToLower());
}

public async Task<string> GetUserIdAsync(CustomUser user, CancellationToken cancellationToken)
{
var u = await FindByNameAsync(user.UserName, cancellationToken);
return u.Id.ToString();
}

public async Task<string> GetUserNameAsync(CustomUser user, CancellationToken cancellationToken)
{
var u = await FindByIdAsync(user.Id.ToString(), cancellationToken);
return u.UserName;
}

public async Task SetNormalizedUserNameAsync(CustomUser user, string normalizedName,
CancellationToken cancellationToken)
{
await SetUserNameAsync(user, normalizedName, cancellationToken);
}

public async Task SetUserNameAsync(CustomUser user, string userName, CancellationToken cancellationToken)
{
var u = await FindByIdAsync(user.Id.ToString(), cancellationToken);
u.UserName = userName.ToLower();
}

public Task<IdentityResult> UpdateAsync(CustomUser user, CancellationToken cancellationToken)
{
return Task.FromResult(IdentityResult.Failed());
}

public Task<string> GetPasswordHashAsync(CustomUser user, CancellationToken cancellationToken)
{
return Task.FromResult("");
}

public Task<bool> HasPasswordAsync(CustomUser user, CancellationToken cancellationToken)
{
return Task.FromResult(true);
}

public Task SetPasswordHashAsync(CustomUser user, string passwordHash, CancellationToken cancellationToken)
{
return Task.FromResult(0);
}

public Task<IList<Claim>> GetClaimsAsync(CustomUser user, CancellationToken cancellationToken)
{
if(user.Id == 1)
return Task.FromResult((IList<Claim>) new List<Claim>() {new Claim("VIPNumber", "123")});
return Task.FromResult((IList<Claim>) new List<Claim>());
}

public Task AddClaimsAsync(CustomUser user, IEnumerable<Claim> claims, CancellationToken cancellationToken)
{
return Task.FromResult(0);
}

public Task ReplaceClaimAsync(CustomUser user, Claim claim, Claim newClaim, CancellationToken cancellationToken)
{
return Task.FromResult(0);
}

public Task RemoveClaimsAsync(CustomUser user, IEnumerable<Claim> claims, CancellationToken cancellationToken)
{
return Task.FromResult(0);
}

public Task<IList<CustomUser>> GetUsersForClaimAsync(Claim claim, CancellationToken cancellationToken)
{
return Task.FromResult((IList<CustomUser>) users);
}
}
}
@@ -0,0 +1,19 @@
using System.Security.Claims;
using System.Security.Principal;

namespace PrairieCodeIdentity.Identity
{
//public class CustomUser : ClaimsPrincipal
//{
// public CustomUser(string username)
// {
// AddIdentity(new GenericIdentity(username));
// }
//}

public class CustomUser
{
public int Id { get; set; }
public string UserName { get; set; }
}
}
@@ -0,0 +1,7 @@
namespace PrairieCodeIdentity.Identity
{
public class LoginViewModel
{
public string UserName { get; set; }
}
}
@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Identity;

namespace PrairieCodeIdentity.Identity
{
public class NoPasswordHasher : IPasswordHasher<CustomUser>
{
public string HashPassword(CustomUser user, string password)
{
return password;
}

public PasswordVerificationResult VerifyHashedPassword(CustomUser user, string hashedPassword,
string providedPassword)
{
return PasswordVerificationResult.Success;
}
}
}

0 comments on commit abea502

Please sign in to comment.