public async Task <IHttpActionResult> GetTokenAsync([FromBody] User inputData)
        {
            AuthorizationToken result = await Task <AuthorizationToken> .Factory.StartNew(
                (a) =>
            {
                User user = a as User;
                try
                {
                    using (LicensingServerDB dbContext = new LicensingServerDB())
                    {
                        user = dbContext.Users.FindAsync(user.UserID).Result;
                        AuthorizationToken authorizationToken = new AuthorizationToken
                        {
                            UserID         = user.UserID,
                            ExpirationDate = DateTime.Today.AddDays(1)
                        };
                        authorizationToken.GenerateTokenValue();
                        authorizationToken = dbContext.AuthorizationTokens.Add(authorizationToken);
                        dbContext.SaveChanges();
                        return(authorizationToken);
                    }
                }
                catch
                {
                }
                return(null);
            },
                inputData);

            return(Ok(result));
        }
Beispiel #2
0
        public override Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            TaskFactory taskFactory = new TaskFactory(cancellationToken);

            return(taskFactory.StartNew(async() =>
            {
                await base.OnAuthorizationAsync(actionContext, cancellationToken);
                var tokenValue = actionContext.Request.Headers.Authorization.Parameter;
                using (LicensingServerDB dbContext = new LicensingServerDB())
                {
                    var findedTokens = dbContext.AuthorizationTokens.Where(x => x.TokenValue == tokenValue);
                    if (findedTokens.Any())
                    {
                        AuthorizationToken authorizationToken = findedTokens.OrderByDescending(x => x.ExpirationDate).First();
                        if (authorizationToken.ExpirationDate < DateTime.Today)
                        {
                            actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden);
                            return;
                        }
                        return;
                    }
                }
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }));
        }
Beispiel #3
0
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            var tokenValue = actionContext.Request.Headers.Authorization.Parameter;

            using (LicensingServerDB dbContext = new LicensingServerDB())
            {
                var findedTokens = dbContext.AuthorizationTokens.Where(x => x.TokenValue == tokenValue);
                if (findedTokens.Any())
                {
                    AuthorizationToken authorizationToken = findedTokens.OrderByDescending(x => x.ExpirationDate).First();
                    if (authorizationToken.ExpirationDate < DateTime.Today)
                    {
                        return(false);
                    }
                    return(true);
                }
            }
            return(false);
        }
Beispiel #4
0
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            base.OnAuthorization(actionContext);
            var tokenValue = actionContext.Request.Headers.Authorization.Parameter;

            using (LicensingServerDB dbContext = new LicensingServerDB())
            {
                var findedTokens = dbContext.AuthorizationTokens.Where(x => x.TokenValue == tokenValue);
                if (findedTokens.Any())
                {
                    AuthorizationToken authorizationToken = findedTokens.OrderByDescending(x => x.ExpirationDate).First();
                    if (authorizationToken.ExpirationDate < DateTime.Today)
                    {
                        actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden);
                        return;
                    }
                    return;
                }
            }
            actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
        }