Example #1
0
        /// <summary>
        /// Function that will do Authentication and Authenticationorization
        /// </summary>
        /// <param name="context">Current HttpListenerContext </param>
        /// <param name="Schemes">AuthenticationSchemes</param>
        /// <exception cref="ChannelCredentialsException"></exception>
        /// <returns>True if user is Authenticationenticated and Authenticationorized , False if not</returns>
        public bool AuthenticatedAndAuthorized(HttpListenerContext context, ChannelAuthenticationSchemes Schemes)
        {
            if (Schemes == ChannelAuthenticationSchemes.Token)
            {
                string token   = string.Empty;
                bool   isToken = HttpListenerIdentityMiddleware.IsTokenHeader(context.Request, out token);
                if (isToken)
                {
                    IPrincipal tokenIdentity = HttpListenerIdentityMiddleware.ParseTokenAuthentication(token);
                    return(AuthenticateRequest(tokenIdentity.Identity, Schemes));
                }
                else
                {
                    throw new ChannelCredentialsException("Malformed or missing header for the token Authentication");
                }
            }
            else
            {
                string username = string.Empty;
                string password = string.Empty;
                bool   isBasic  = HttpListenerIdentityMiddleware.IsBasicHeader(context.Request, out username, out password);

                if (isBasic)
                {
                    IPrincipal basicIdentity = HttpListenerIdentityMiddleware.ParseBasicAuthentication(username, password);
                    return(AuthenticateRequest(basicIdentity.Identity, ChannelAuthenticationSchemes.Basic));
                }
                throw new ChannelCredentialsException("Malformed or missing header for the basic Authentication");
            }
        }
        public IPrincipal GetTokenPrincipal(out string token)
        {
            bool isToken = HttpListenerIdentityMiddleware.IsTokenHeader(_context.Context.Request, out token);

            if (isToken)
            {
                return(HttpListenerIdentityMiddleware.ParseTokenAuthentication(token));
            }
            else
            {
                return(null);
            }
        }
        public IPrincipal GetBasicPrincipal(out string username, out string password)
        {
            bool isBasic = HttpListenerIdentityMiddleware.IsBasicHeader(_context.Context.Request, out username, out password);

            if (isBasic)
            {
                return(HttpListenerIdentityMiddleware.ParseBasicAuthentication(username, password));
            }
            else
            {
                return(null);
            }
        }