forked from Clark159/CLK.AspNetCoreLab
-
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.
Add CookieOrJwtBearerAuthenticationLab
- Loading branch information
Showing
48 changed files
with
921 additions
and
947 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
File renamed without changes.
File renamed without changes.
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
212 changes: 212 additions & 0 deletions
212
src/CookieOrJwtBearerAuthenticationLab/Controllers/AccountController.cs
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,212 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
|
||
namespace CookieOrJwtBearerAuthenticationLab | ||
{ | ||
[AllowAnonymous] | ||
public partial class AccountController : Controller | ||
{ | ||
// Fields | ||
private readonly SecurityTokenFactory _tokenFactory; | ||
|
||
|
||
// Constructors | ||
public AccountController(SecurityTokenFactory tokenFactory) | ||
{ | ||
#region Contracts | ||
|
||
if (tokenFactory == null) throw new ArgumentException(nameof(tokenFactory)); | ||
|
||
#endregion | ||
|
||
// Default | ||
_tokenFactory = tokenFactory; | ||
} | ||
|
||
|
||
// Methods | ||
public async Task<ActionResult> Login(string username = null, string password = null, string returnUrl = @"/") | ||
{ | ||
// Require | ||
if (string.IsNullOrEmpty(username) == true) return View(); | ||
if (string.IsNullOrEmpty(returnUrl) == true) returnUrl = @"/"; | ||
if (this.User.Identity.IsAuthenticated == true) return this.Redirect(returnUrl); | ||
|
||
// Validate | ||
// ... | ||
|
||
// ClaimsPrincipal | ||
var claimIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme); | ||
claimIdentity.AddClaim(new Claim(ClaimTypes.Name, username)); | ||
var claimsPrincipal = new ClaimsPrincipal(claimIdentity); | ||
|
||
// SignIn | ||
await this.HttpContext.SignInAsync(claimsPrincipal); | ||
|
||
// Redirect | ||
return this.Redirect(returnUrl); | ||
} | ||
|
||
public async Task<ActionResult> Logout() | ||
{ | ||
// Require | ||
if (this.User.Identity.IsAuthenticated == false) return this.Redirect(@"/"); | ||
|
||
// SignIn | ||
await this.HttpContext.SignOutAsync(); | ||
|
||
// Redirect | ||
return this.Redirect(@"/"); | ||
} | ||
} | ||
|
||
// GetUser | ||
public partial class AccountController : Controller | ||
{ | ||
// Methods | ||
[Authorize] | ||
public ActionResult<GetUserResultModel> GetUser([FromBody] GetUserActionModel actionModel) | ||
{ | ||
#region Contracts | ||
|
||
if (actionModel == null) throw new ArgumentException(nameof(actionModel)); | ||
|
||
#endregion | ||
|
||
// UserModel | ||
var user = new UserModel(); | ||
user.Username = this.User.Identity.Name; | ||
user.AuthenticationType = this.User.Identity.AuthenticationType; | ||
|
||
// Return | ||
return (new GetUserResultModel() | ||
{ | ||
User = user | ||
}); | ||
} | ||
|
||
|
||
// Class | ||
public class GetUserActionModel | ||
{ | ||
// Properties | ||
|
||
} | ||
|
||
public class GetUserResultModel | ||
{ | ||
// Properties | ||
public UserModel User { get; set; } | ||
} | ||
|
||
public class UserModel | ||
{ | ||
// Properties | ||
public string Username { get; set; } | ||
|
||
public string AuthenticationType { get; set; } | ||
} | ||
} | ||
|
||
// GetToken | ||
public partial class AccountController : Controller | ||
{ | ||
// Methods | ||
[Authorize] | ||
public ActionResult<GetTokenResultModel> GetToken([FromBody] GetTokenActionModel actionModel) | ||
{ | ||
#region Contracts | ||
|
||
if (actionModel == null) throw new ArgumentException(nameof(actionModel)); | ||
|
||
#endregion | ||
|
||
// ClaimIdentity | ||
var claimIdentity = this.User.Identity as ClaimsIdentity; | ||
if (claimIdentity == null) throw new InvalidOperationException($"{nameof(claimIdentity)}=null"); | ||
|
||
// TokenString | ||
var tokenString = _tokenFactory.CreateEncodedJwt(claimIdentity); | ||
if (string.IsNullOrEmpty(tokenString) == true) throw new InvalidOperationException($"{nameof(tokenString)}=null"); | ||
|
||
// Return | ||
return (new GetTokenResultModel() | ||
{ | ||
Token = tokenString | ||
}); | ||
} | ||
|
||
|
||
// Class | ||
public class GetTokenActionModel | ||
{ | ||
// Properties | ||
|
||
} | ||
|
||
public class GetTokenResultModel | ||
{ | ||
// Properties | ||
public string Token { get; set; } | ||
} | ||
} | ||
|
||
// GetTokenByPassword | ||
public partial class AccountController : Controller | ||
{ | ||
// Methods | ||
[AllowAnonymous] | ||
public ActionResult<GetTokenByPasswordResultModel> GetTokenByPassword([FromBody] GetTokenByPasswordActionModel actionModel) | ||
{ | ||
#region Contracts | ||
|
||
if (actionModel == null) throw new ArgumentException(nameof(actionModel)); | ||
|
||
#endregion | ||
|
||
// Require | ||
if (string.IsNullOrEmpty(actionModel.Username) == true) throw new InvalidOperationException($"{nameof(actionModel.Username)}=null"); | ||
if (string.IsNullOrEmpty(actionModel.Password) == true) throw new InvalidOperationException($"{nameof(actionModel.Password)}=null"); | ||
|
||
// Validate | ||
// ... | ||
|
||
// ClaimIdentity | ||
var claimIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme); | ||
claimIdentity.AddClaim(new Claim(ClaimTypes.Name, actionModel.Username)); | ||
|
||
// TokenString | ||
var tokenString = _tokenFactory.CreateEncodedJwt(claimIdentity.Claims); | ||
if (string.IsNullOrEmpty(tokenString) == true) throw new InvalidOperationException($"{nameof(tokenString)}=null"); | ||
|
||
// Return | ||
return (new GetTokenByPasswordResultModel() | ||
{ | ||
Token = tokenString | ||
}); | ||
} | ||
|
||
|
||
// Class | ||
public class GetTokenByPasswordActionModel | ||
{ | ||
// Properties | ||
public string Username { get; set; } | ||
|
||
public string Password { get; set; } | ||
} | ||
|
||
public class GetTokenByPasswordResultModel | ||
{ | ||
// Properties | ||
public string Token { get; set; } | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/CookieOrJwtBearerAuthenticationLab/Controllers/HomeController.cs
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 Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace CookieOrJwtBearerAuthenticationLab | ||
{ | ||
public class HomeController : Controller | ||
{ | ||
// Methods | ||
[Authorize] | ||
public ActionResult Index() | ||
{ | ||
// Return | ||
return View(); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/CookieOrJwtBearerAuthenticationLab/CookieOrJwtBearerAuthenticationLab.csproj
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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.