forked from gitter-badger/IdentityServer4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentityServerTools.cs
More file actions
60 lines (51 loc) · 2.05 KB
/
IdentityServerTools.cs
File metadata and controls
60 lines (51 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using IdentityServer4.Models;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using IdentityServer4.Extensions;
using System.Security.Claims;
using IdentityServer4.Services;
using IdentityModel;
using System;
namespace IdentityServer4
{
/// <summary>
/// Class for useful helpers for interacting with IdentityServer
/// </summary>
public class IdentityServerTools
{
internal readonly IHttpContextAccessor _contextAccessor;
private readonly ITokenCreationService _tokenCreation;
/// <summary>
/// Initializes a new instance of the <see cref="IdentityServerTools"/> class.
/// </summary>
/// <param name="contextAccessor">The context accessor.</param>
/// <param name="tokenCreation">The token creation service.</param>
public IdentityServerTools(IHttpContextAccessor contextAccessor, ITokenCreationService tokenCreation)
{
_tokenCreation = tokenCreation;
_contextAccessor = contextAccessor;
}
/// <summary>
/// Issues a JWT.
/// </summary>
/// <param name="lifetime">The lifetime.</param>
/// <param name="claims">The claims.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">claims</exception>
public virtual async Task<string> IssueJwtAsync(int lifetime, IEnumerable<Claim> claims)
{
if (claims == null) throw new ArgumentNullException(nameof(claims));
var issuer = _contextAccessor.HttpContext.GetIdentityServerIssuerUri();
var token = new Token
{
Issuer = issuer,
Lifetime = lifetime,
Claims = new HashSet<Claim>(claims, new ClaimComparer())
};
return await _tokenCreation.CreateTokenAsync(token);
}
}
}