예제 #1
0
        internal static IPrincipal CreateUser(
            string response,
            AuthenticationSchemes scheme,
            string realm,
            string method,
            Func <IIdentity, NetworkCredential> credentialsFinder)
        {
            if (response == null || response.Length == 0)
            {
                return(null);
            }

            if (credentialsFinder == null)
            {
                return(null);
            }

            if (!(scheme == AuthenticationSchemes.Basic || scheme == AuthenticationSchemes.Digest))
            {
                return(null);
            }

            if (scheme == AuthenticationSchemes.Digest)
            {
                if (realm == null || realm.Length == 0)
                {
                    return(null);
                }

                if (method == null || method.Length == 0)
                {
                    return(null);
                }
            }

            if (!response.StartsWith(scheme.ToString(), StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            var res = AuthenticationResponse.Parse(response);

            if (res == null)
            {
                return(null);
            }

            var id = res.ToIdentity();

            if (id == null)
            {
                return(null);
            }

            NetworkCredential cred = null;

            try
            {
                cred = credentialsFinder(id);
            }
            catch
            {
            }

            if (cred == null)
            {
                return(null);
            }

            if (scheme == AuthenticationSchemes.Basic && ((HttpBasicIdentity)id).Password != cred.Password)
            {
                return(null);
            }

            if (scheme == AuthenticationSchemes.Digest && !((HttpDigestIdentity)id).IsValid(cred.Password, realm, method, null))
            {
                return(null);
            }

            return(new GenericPrincipal(id, cred.Roles));
        }
예제 #2
0
        internal static IPrincipal CreateUser(
            string response,
            AuthenticationSchemes scheme,
            string realm,
            string method,
            Func <IIdentity, NetworkCredential> credentialsFinder
            )
        {
            if (response == null || response.Length == 0)
            {
                return(null);
            }

            if (scheme == AuthenticationSchemes.Digest)
            {
                if (realm == null || realm.Length == 0)
                {
                    return(null);
                }

                if (method == null || method.Length == 0)
                {
                    return(null);
                }
            }
            else
            {
                if (scheme != AuthenticationSchemes.Basic)
                {
                    return(null);
                }
            }

            if (credentialsFinder == null)
            {
                return(null);
            }

            var compType = StringComparison.OrdinalIgnoreCase;

            if (response.IndexOf(scheme.ToString(), compType) != 0)
            {
                return(null);
            }

            var res = AuthenticationResponse.Parse(response);

            if (res == null)
            {
                return(null);
            }

            var id = res.ToIdentity();

            if (id == null)
            {
                return(null);
            }

            NetworkCredential cred = null;

            try {
                cred = credentialsFinder(id);
            }
            catch {
            }

            if (cred == null)
            {
                return(null);
            }

            if (scheme == AuthenticationSchemes.Basic)
            {
                var basicId = (HttpBasicIdentity)id;
                return(basicId.Password == cred.Password
               ? new GenericPrincipal(id, cred.Roles)
               : null);
            }

            var digestId = (HttpDigestIdentity)id;

            return(digestId.IsValid(cred.Password, realm, method, null)
             ? new GenericPrincipal(id, cred.Roles)
             : null);
        }
        internal static AuthenticationResponse Parse(string value)
        {
            AuthenticationResponse authenticationResponse;
            AuthenticationResponse authenticationResponse1;

            try
            {
                string[] strArrays = value.Split(new char[] { ' ' }, 2);
                if ((int)strArrays.Length == 2)
                {
                    string lower = strArrays[0].ToLower();
                    if (lower == "basic")
                    {
                        authenticationResponse1 = new AuthenticationResponse(AuthenticationSchemes.Basic, AuthenticationResponse.ParseBasicCredentials(strArrays[1]));
                    }
                    else if (lower == "digest")
                    {
                        authenticationResponse1 = new AuthenticationResponse(AuthenticationSchemes.Digest, AuthenticationBase.ParseParameters(strArrays[1]));
                    }
                    else
                    {
                        authenticationResponse1 = null;
                    }
                    authenticationResponse = authenticationResponse1;
                    return(authenticationResponse);
                }
                else
                {
                    authenticationResponse = null;
                    return(authenticationResponse);
                }
            }
            catch
            {
            }
            authenticationResponse = null;
            return(authenticationResponse);
        }
        internal static string CreateRequestDigest(NameValueCollection parameters)
        {
            string item  = parameters["username"];
            string str   = parameters["password"];
            string item1 = parameters["realm"];
            string str1  = parameters["nonce"];
            string item2 = parameters["uri"];
            string str2  = parameters["algorithm"];
            string item3 = parameters["qop"];
            string str3  = parameters["cnonce"];
            string item4 = parameters["nc"];
            string str4  = parameters["method"];
            string str5  = (str2 == null || !(str2.ToLower() == "md5-sess") ? AuthenticationResponse.createA1(item, str, item1) : AuthenticationResponse.createA1(item, str, item1, str1, str3));
            string str6  = (item3 == null || !(item3.ToLower() == "auth-int") ? AuthenticationResponse.createA2(str4, item2) : AuthenticationResponse.createA2(str4, item2, parameters["entity"]));
            string str7  = AuthenticationResponse.hash(str5);
            string str8  = (item3 != null ? string.Format("{0}:{1}:{2}:{3}:{4}", new object[] { str1, item4, str3, item3, AuthenticationResponse.hash(str6) }) : string.Format("{0}:{1}", str1, AuthenticationResponse.hash(str6)));
            string str9  = AuthenticationResponse.hash(string.Format("{0}:{1}", str7, str8));

            return(str9);
        }
        private static string createA2(string method, string uri, string entity)
        {
            string str = string.Format("{0}:{1}:{2}", method, uri, AuthenticationResponse.hash(entity));

            return(str);
        }
        private static string createA1(string username, string password, string realm, string nonce, string cnonce)
        {
            string str = string.Format("{0}:{1}:{2}", AuthenticationResponse.hash(AuthenticationResponse.createA1(username, password, realm)), nonce, cnonce);

            return(str);
        }