コード例 #1
0
        public LoginPacket(byte[] input)
        {
            // Get the opcode.
            OpCode = (LoginOp)BitConverter.ToUInt16(input, 0);

            Data = new byte[input.Length - sizeof(ushort)];
            Buffer.BlockCopy(input, sizeof(ushort), Data, 0, Data.Length);
        }
コード例 #2
0
        private async Task GenerateToken(HttpContext context)
        {
            var email    = context.Request.Form["email"];
            var password = context.Request.Form["password"];

            var loginOp = new LoginOp(_unitOfWork);

            User user;

            try
            {
                user = (User)loginOp.Execute(new LoginModel {
                    Email = email, Password = password
                });
                var identity = await _options.IdentityResolver(user);
            }
            catch (Exception ex)
            {
                context.Response.StatusCode  = 400;
                context.Response.ContentType = "application/json";

                var jsonException = ExceptionHandler.FormatException(ex);
                var json          = JsonConvert.SerializeObject(jsonException, _serializerSettings);
                await context.Response.WriteAsync(json);

                return;
            }

            var now = DateTime.UtcNow;

            // Specifically add the jti (nonce), iat (issued timestamp), and sub (subject/user) claims.
            // You can add other claims here, if you want:
            var claims = new Claim[]
            {
                new Claim("userId", user.Id.ToString()),
                new Claim("email", email)
            };

            // Create the JWT and write it to a string
            var jwt = new JwtSecurityToken(
                _options.Issuer,
                _options.Audience,
                claims,
                notBefore: now,
                expires: now.Add(_options.Expiration),
                signingCredentials: _options.SigningCredentials);
            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            var response = new
            {
                access_token = encodedJwt,
                expires_in   = (int)_options.Expiration.TotalSeconds
            };

            // Serialize and return the response
            context.Response.ContentType = "application/json";
            await context.Response.WriteAsync(JsonConvert.SerializeObject(response, _serializerSettings));
        }