-
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.
- Loading branch information
Showing
36 changed files
with
1,278 additions
and
313 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
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,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace N2N.Api.Configuration | ||
{ | ||
public class TokenConfig | ||
{ | ||
public const string ISSUER = "MyAuthServer"; // издатель токена | ||
public const string AUDIENCE = "*"; // потребитель токена | ||
const string KEY = "mysupersecret_secretkey!123"; // ключ для шифрации | ||
public const int LIFETIME = 60; // время жизни токена (180 минут рекомендуемое) | ||
public static SymmetricSecurityKey GetSymmetricSecurityKey() | ||
{ | ||
return new SymmetricSecurityKey(Encoding.ASCII.GetBytes(KEY)); | ||
} | ||
} | ||
} |
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,77 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using IdentityServer4.Extensions; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.ApplicationModels; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using N2N.Api.Services; | ||
using N2N.Infrastructure.Models; | ||
using N2N.Services; | ||
|
||
namespace N2N.Api.Filters | ||
{ | ||
public class N2NAutorizationFilterAttribute : ActionFilterAttribute | ||
{ | ||
|
||
|
||
|
||
public override void OnActionExecuting(ActionExecutingContext context) | ||
{ | ||
|
||
var service = context.HttpContext.RequestServices.GetService<IAuthentificationService>(); | ||
|
||
|
||
ObjectResult result = new ObjectResult(""); | ||
|
||
var authHeader = context.HttpContext.Request.Headers["Authorization"]; | ||
|
||
|
||
if (authHeader.IsNullOrEmpty()) | ||
{ | ||
result = new ObjectResult("you do not have Authorization header"); | ||
result.StatusCode = 401; | ||
} | ||
else | ||
{ | ||
|
||
var tokenValidationResult = service.ValidateTokenString(authHeader.ToString()); | ||
|
||
if (!tokenValidationResult.Success) | ||
{ | ||
result = new ObjectResult(tokenValidationResult.Messages); | ||
result.StatusCode = 401; | ||
} | ||
} | ||
|
||
context.Result = result; | ||
} | ||
|
||
public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | ||
{ | ||
|
||
return base.OnActionExecutionAsync(context, next); | ||
} | ||
|
||
public override void OnResultExecuted(ResultExecutedContext context) | ||
{ | ||
base.OnResultExecuted(context); | ||
} | ||
|
||
public override void OnResultExecuting(ResultExecutingContext context) | ||
{ | ||
base.OnResultExecuting(context); | ||
} | ||
|
||
public override Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) | ||
{ | ||
return base.OnResultExecutionAsync(context, next); | ||
} | ||
|
||
|
||
} | ||
} |
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.