public async virtual Task ValidateIdentity(ApiKeyValidateIdentityContext ctx)
 {
     if (ctx.ApiKey == "123456")
     {
         ctx.Validate();
     }
 }
 private async Task ValidateApiKey(ApiKeyValidateIdentityContext context)
 {
     if (context.ApiKey == "123")
     {
         context.Validate();
     }
 }
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            string authorizationHeader = this.Request.Headers.Get(this.Options.Header);

            if (!String.IsNullOrWhiteSpace(authorizationHeader))
            {
                if (authorizationHeader.StartsWith(this.Options.HeaderKey, StringComparison.OrdinalIgnoreCase))
                {
                    string apiKey = authorizationHeader.Substring(this.Options.HeaderKey.Length).Trim();

                    var context = new ApiKeyValidateIdentityContext(this.Context, this.Options, apiKey);

                    await this.Options.Provider.ValidateIdentity(context);

                    if (context.IsValidated)
                    {
                        var claims = await this.Options.Provider.GenerateClaims(new ApiKeyGenerateClaimsContext(this.Context, this.Options, apiKey));

                        var identity = new ClaimsIdentity(claims, this.Options.AuthenticationType);

                        return(new AuthenticationTicket(identity, new AuthenticationProperties()
                        {
                            IssuedUtc = DateTime.UtcNow
                        }));
                    }
                }
            }

            return(null);
        }
 private async Task ValidateIdentity(ApiKeyValidateIdentityContext context)
 {
     if (ApiKeyController.IsValidKey(context.ApiKey))
     {
         context.Validate();
     }
 }
        public static async Task ValidateIdentity(ApiKeyValidateIdentityContext context)
        {
            var  user = WebApiConfiguration.Instance.Users.Where(u => u.ApiKey == context.ApiKey).FirstOrDefault();
            bool IpOk = false;

            if (user != null)
            {
                if (user.IpAddresses.Count == 0)
                {
                    IpOk = true;
                }
                else
                {
                    IPAddress clientIPAddress = IPAddress.Parse(context.Request.RemoteIpAddress);

                    foreach (var userIP in user.IpAddresses)
                    {
                        IpOk = IPAddress.TryParse(userIP, out IPAddress userIPAddress) && userIPAddress.Equals(clientIPAddress);
                        if (IpOk)
                        {
                            break;
                        }
                    }
                }

                if (IpOk)
                {
                    context.Validate();
                }
            }
        }
Example #6
0
        /// <summary>
        /// Implements the interface method by invoking the related delegate method.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual async Task ValidateIdentity(ApiKeyValidateIdentityContext context)
        {
            if (this.OnValidateIdentity == null)
            {
                throw new ArgumentNullException(nameof(this.OnValidateIdentity), "You must pass a delegate to OnValidateIdentity in order to validate your incoming API keys.");
            }

            await this.OnValidateIdentity(context);
        }
Example #7
0
        private Task ValidateIdentity(ApiKeyValidateIdentityContext context)
        {
            var authenticationService = context.OwinContext.Environment["AspNet.Identity.Owin:myfoodapp.Hub.Providers.IAuthenticationProvider, myfoodapp.Hub, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"]
                                        as IAuthenticationProvider;

            if (authenticationService.IsApiKeyAllowed(context.ApiKey))
            {
                context.Validate();
            }

            return(Task.FromResult(true));
        }
        private static Task ValidateIdentity(ApiKeyValidateIdentityContext context)
        {
            if (context.ApiKey == "123")
            {
                context.Validate();
            }
            else if (context.ApiKey == "789")
            {
                context.RewriteStatusCode = true;
                context.StatusCode        = HttpStatusCode.UpgradeRequired;
            }

            return(Task.FromResult(0));
        }
Example #9
0
        /// <summary>
        /// Check that the key in the identity context is valid
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static async Task ValidateIdentity(ApiKeyValidateIdentityContext context)
        {
            using (var db = new ZapContext())
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

                var submittedKey = context.ApiKey;

                var isValidKey = await db.APIKeys
                                 .Where(k => k.Key == submittedKey)
                                 .AnyAsync().ConfigureAwait(true);

                if (isValidKey)
                {
                    context.Validate();
                }
            }
        }
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            if (this.Request.Headers.ContainsKey(this.Options.Header))
            {
                string authorizationHeader = this.Request.Headers.Get(this.Options.Header);

                if (!string.IsNullOrWhiteSpace(authorizationHeader))
                {
                    if (Options.HeaderKeyArray == null && Options.HeaderKey != null)
                    {
                        Options.HeaderKeyArray = new[] { Options.HeaderKey };
                    }
                    if (Options.HeaderKeyArray != null && Options.HeaderKeyArray.Length > 0)
                    {
                        var headerKeyFound = false;
                        foreach (var headerKey in this.Options.HeaderKeyArray)
                        {
                            if (authorizationHeader.StartsWith(headerKey, StringComparison.OrdinalIgnoreCase))
                            {
                                headerKeyFound = true;
                                string apiKey = authorizationHeader.Substring(headerKey.Length).Trim();
                                if (!string.IsNullOrEmpty(apiKey))
                                {
                                    var context = new ApiKeyValidateIdentityContext(this.Context, this.Options, apiKey);

                                    await this.Options.Provider.ValidateIdentity(context);

                                    if (context.IsValidated)
                                    {
                                        var claims =
                                            await this.Options.Provider.GenerateClaims(
                                                new ApiKeyGenerateClaimsContext(this.Context, this.Options, apiKey));

                                        var identity = new ClaimsIdentity(claims, this.Options.AuthenticationType);

                                        return(new AuthenticationTicket(identity, new AuthenticationProperties()
                                        {
                                            IssuedUtc = DateTime.UtcNow
                                        }));
                                    }
                                }
                                else
                                {
                                    throw new ArgumentNullException(nameof(Options.HeaderKey), "ApiKey not found");
                                }
                            }
                        }
                        if (!headerKeyFound)
                        {
                            throw new InvalidCredentialException("Header Key Not Supported");
                        }
                    }
                    else
                    {
                        throw new ArgumentNullException(nameof(Options.HeaderKey), "No Header Key( eg: Apikey) Found.");
                    }
                }
                else
                {
                    throw new ArgumentNullException(nameof(Options.Header), "Authorization Header is Empty.");
                }
            }
            return(null);
        }