Exemple #1
0
        public void TestCreate_Null()
        {
            // todo: send in http status code so we can consolidate error validation
            var userRealm = UserRealm.Create(null);

            Assert.Fail();
        }
Exemple #2
0
        private static UserRealm Registration(DataBaseHelper _dbHelper)
        {
            var result = new UserRealm();

            Console.WriteLine("Введите логин : ");
            result.Login = Console.ReadLine();
            if (_dbHelper.UserContains(result.Login))
            {
                Console.WriteLine("Пользователь с таким логином существует. Ппопробуйте что-то новое." + Environment.NewLine);
                return(Registration(_dbHelper));
            }
            Console.WriteLine("Введите пароль : ");
            result.Password = Encoding.UTF8.GetString((new SHA1CryptoServiceProvider()).ComputeHash(Encoding.UTF8.GetBytes(Console.ReadLine())));
            _dbHelper.SaveUser(result);
            return(result);
        }
Exemple #3
0
        private static void ShowList(UserRealm user, DataBaseHelper dbHelper)
        {
            Console.WriteLine("Записи пользователя в БД : ");
            var listnotes = dbHelper.GetAllNotes(user.UserKey);

            if (listnotes != null || listnotes.Count == 0)
            {
                foreach (var note in listnotes)
                {
                    Console.WriteLine(note.NoteKey + " | " + note.Title + " | " + note.CreateDate + " | " + note.ContentText);
                }
            }
            else
            {
                Console.WriteLine("Нет записей");
            }
        }
Exemple #4
0
        private static void AddNotes(UserRealm user, DataBaseHelper dbHelper)
        {
            var newNote = new NoteRealm();

            newNote.UserId = user.UserKey;
            Console.WriteLine("Введите название записи : ");
            newNote.Title = Console.ReadLine();
            Console.WriteLine("Введите саму запись : ");
            newNote.ContentText = Console.ReadLine();
            dbHelper.SaveNote(newNote);
            Console.WriteLine("Хотите добавить еще запись ? y/n");
            var result = Console.ReadKey();

            if (result.KeyChar == 'y')
            {
                Console.WriteLine();
                AddNotes(user, dbHelper);
            }
        }
Exemple #5
0
        private async Task <UserRealm> GetUserRealm(string commonRequestGuid)
        {
            UserRealm userRealm = new UserRealm();

            using (var client = new HttpClient())
            {
                string userRealmUri = $"https://login.windows.net/common/UserRealm/{_powerBiOptions.Username}?api-version=1.0";

                HttpRequestMessage realmRequest = new HttpRequestMessage(HttpMethod.Get, userRealmUri);
                realmRequest.Headers.Add("Accept", "application/json");
                realmRequest.Headers.Add("return-client-request-id", "true");
                realmRequest.Headers.Add("client-request-id", commonRequestGuid);

                HttpResponseMessage realmResponse = client.SendAsync(realmRequest).Result;
                string realmString = await realmResponse.Content.ReadAsStringAsync();

                userRealm = JsonConvert.DeserializeObject <UserRealm>(realmString);
            }
            return(userRealm);
        }
Exemple #6
0
        private async Task <OAuthResult> GetAuthToken()
        {
            string      commonRequestGuid = Guid.NewGuid().ToString();
            OAuthResult oauthResult;

            UserRealm userRealm = await GetUserRealm(commonRequestGuid);

            if (userRealm.account_type.Equals("Federated"))
            {
                XmlDocument metadata
                    = await GetFederationMetadata(commonRequestGuid, userRealm.federation_metadata_url);

                string      trustBinding = GetFederatedUserTrustBinding(metadata);
                XmlDocument trustDocument
                    = await GetFederatedUserTrust(commonRequestGuid, trustBinding);

                var userAssertionNodes = trustDocument.GetElementsByTagName("saml:Assertion");
                var userAssertionNode  = userAssertionNodes[0].OuterXml;

                using (var client = new HttpClient())
                {
                    string tokenUri = "https://login.windows.net/common/oauth2/token";
                    var    ua       = new UserAssertion(
                        userAssertionNode,
                        "urn:ietf:params:oauth:grant-type:saml1_1-bearer",
                        Uri.EscapeDataString(_powerBiOptions.Username));

                    UTF8Encoding encoding   = new UTF8Encoding();
                    Byte[]       byteSource = encoding.GetBytes(ua.Assertion);
                    string       base64ua   = Convert.ToBase64String(byteSource);

                    var tokenForm = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair <string, string>("resource", _powerBiOptions.ResourceUrl),
                        new KeyValuePair <string, string>("client_id", _powerBiOptions.ClientId),
                        new KeyValuePair <string, string>("grant_type", "urn:ietf:params:oauth:grant-type:saml1_1-bearer"),
                        new KeyValuePair <string, string>("assertion", base64ua),
                        new KeyValuePair <string, string>("scope", "openid"),
                    });

                    var tokenResult = await client.PostAsync(tokenUri, tokenForm);

                    var tokenContent = await tokenResult.Content.ReadAsStringAsync();

                    oauthResult = JsonConvert.DeserializeObject <OAuthResult>(tokenContent);
                }
            }
            else
            {
                using (var client = new HttpClient())
                {
                    var result = await client.PostAsync(_powerBiOptions.AuthorityUrl, new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair <string, string>("resource", _powerBiOptions.ResourceUrl),
                        new KeyValuePair <string, string>("client_id", _powerBiOptions.ClientId),
                        new KeyValuePair <string, string>("client_secret", _powerBiOptions.ClientSecret),
                        new KeyValuePair <string, string>("grant_type", "password"),
                        new KeyValuePair <string, string>("username", _powerBiOptions.Username),
                        new KeyValuePair <string, string>("password", _powerBiOptions.Password),
                        new KeyValuePair <string, string>("scope", "openid"),
                    }));

                    var tokenContent = await result.Content.ReadAsStringAsync();

                    oauthResult = JsonConvert.DeserializeObject <OAuthResult>(tokenContent);
                }
            }
            return(oauthResult);
        }