Skip to content

Commit

Permalink
Update ReadMe.
Browse files Browse the repository at this point in the history
  • Loading branch information
arichika committed Dec 21, 2015
1 parent 99d326c commit 2299ad9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@
This library supports generating and decoding [JSON Web Tokens](http://tools.ietf.org/html/draft-jones-json-web-token-10).
forked from [jwt-dotnet/jwt](https://github.com/jwt-dotnet/jwt)

## Features
* Support ASP.NET 5 MVC 6 (DNX).
* Two Extention Methods for Converting Unix Timestamp between .NET DateTime.
* Simple usage.

## Installation
Please download and compile it yourself. NuGet is [here](https://www.nuget.org/packages/JwtDnx/).
At first, You need to install Newtonsoft.Json. [FYI](http://www.newtonsoft.com/json).
and, Please download and compile JwtDnx yourself or Install by NuGet,

```console
PM> Install-Package JwtDnx
```
NuGet repo is [here](https://www.nuget.org/packages/JwtDnx/).

## Usage
### Creating Tokens
Expand Down Expand Up @@ -59,16 +70,20 @@ which will output:
As described in the [JWT RFC](https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-32#section-4.1.4) the `exp` "claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing." If an `exp` claim is present and is prior to the current time the token will fail verification. The exp (expiry) value must be specified as the number of seconds since 1/1/1970 UTC.

```csharp
var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var now = Math.Round((DateTime.UtcNow - unixEpoch).TotalSeconds);
var now = DateTime.UtcNow.ToUnixTimeSeconds();
var payload = new Dictionary<string, object>()
{
{ "exp", now }
};
var secretKey = "GQDstcKsx0NHjPOuXOYg5MbeJ1XT0uFiwDVvVBrk";
string token = JwtDnx.JsonWebToken.Encode(payload, secretKey, JwtDnx.JwtHashAlgorithm.HS256);
string jsonPayload = JwtDnx.JsonWebToken.Decode(token, secretKey);
```

if you will decode json that has invalid Unix Timestamp, you'll get some exception.

string jsonPayload = JWT.JsonWebToken.Decode(token, secretKey); // JwtDnx.SignatureVerificationException!
```csharp
string jsonPayload = JwtDnx.JsonWebToken.Decode(token, secretKey); // JwtDnx.SignatureVerificationException!
```

### Configure JSON Serialization
Expand Down
22 changes: 11 additions & 11 deletions test/JwtDnxTests/JwtDnxExtentionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using JwtDnx;
using Xunit;
using JwtDnx;
using Xunit;

namespace JwtDnxTests
{
Expand All @@ -14,18 +14,18 @@ public class ExtentionsTests
private static DateTime _netDateTimeUtc = new DateTime(2038, 01, 19, 03, 14, 07, DateTimeKind.Utc);
private static long _unixTimestamp = 2147483647L;

[Fact]
public void Should_Convert_from_DateTime_to_UnixTime()
{
[Fact]
public void Should_Convert_from_DateTime_to_UnixTime()
{
long result = _netDateTimeUtc.ToUnixTimeSeconds();
Assert.Equal(result, _unixTimestamp);
Assert.Equal(result, _unixTimestamp);
}

[Fact]
public void Should_Convert_from_UnixTime_to_DateTime()
{
[Fact]
public void Should_Convert_from_UnixTime_to_DateTime()
{
DateTime result = _unixTimestamp.ToDateTiemUtc();
Assert.Equal(result, _netDateTimeUtc);
}
Assert.Equal(result, _netDateTimeUtc);
}
}
}

0 comments on commit 2299ad9

Please sign in to comment.