public string Authenticate([FromBody]Common.Models.AuthTokenCredential cred)
        {
            if (cred != null)
            {
                if (!WebMatrix.WebData.WebSecurity.UserExists(cred.UserName))
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));

                if (WebMatrix.WebData.WebSecurity.Login(cred.UserName, cred.Password))
                {
                    if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Session != null
                        && System.Web.HttpContext.Current.Session["_MyAppSession"] != null)
                    {
                        int userId = -1;
                        string token = null;

                        if (WebMatrix.WebData.WebSecurity.IsAuthenticated)
                            userId = WebMatrix.WebData.WebSecurity.GetUserId(WebMatrix.WebData.WebSecurity.CurrentUserName);
                        else
                            userId = WebMatrix.WebData.WebSecurity.GetUserId(cred.UserName);

                        AuthClientData client = new AuthClientData();
                        client.IpAddress = GetIpAddress();
                        client.MachineName = DetermineCompName(client.IpAddress);
                        if (AuthTokenManagerEx.Instance[client] == null)
                        {
                            token = AuthTokenManagerEx.Instance.Generate(client);
                            AuthTokenManagerEx.Instance[client].UserId = userId;
                        }

                        return token;
                    }
                    else
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                }
            }

            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
        }
        public string GetAuthKey(string authData)
        {
            string[] credentials = ParseAuthHeaders(authData);
            if (credentials == null && credentials.Length <= 1)
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable));

            if (!WebMatrix.WebData.WebSecurity.UserExists(credentials[0]))
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));

            if (WebMatrix.WebData.WebSecurity.Login(credentials[0], credentials[1]))
            {
                if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Session != null
                    && System.Web.HttpContext.Current.Session["_MyAppSession"] != null)
                {
                    WebMatrix.WebData.SimpleRoleProvider provider = new WebMatrix.WebData.SimpleRoleProvider();

                    int userId = -1;
                    if (WebMatrix.WebData.WebSecurity.IsAuthenticated)
                        userId = WebMatrix.WebData.WebSecurity.GetUserId(WebMatrix.WebData.WebSecurity.CurrentUserName);
                    else
                        userId = WebMatrix.WebData.WebSecurity.GetUserId(credentials[0]);

                    AuthClientData client = new AuthClientData();
                    client.IpAddress = GetIpAddress();
                    client.MachineName = DetermineCompName(client.IpAddress);
                    string token = AuthTokenManagerEx.Instance.Generate(client);
                    AuthTokenManagerEx.Instance[client].UserId = userId;

                    return token;
                }
                else
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
            }

            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
        }
        public bool IsAuthenticated()
        {
            AuthClientData client = new AuthClientData();
            client.IpAddress = GetIpAddress();
            client.MachineName = DetermineCompName(client.IpAddress);

            return (AuthTokenManagerEx.Instance[client] != null &&
                    AuthTokenManagerEx.Instance[client].GuidKey != null);
        }