コード例 #1
0
ファイル: Global.asax.cs プロジェクト: ionhristiniuc/EduTest
        protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        {
            if (Request.IsAuthenticated)
            {
                if (FormsAuthentication.CookiesSupported)
                {
                    if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
                    {
                        try
                        {
                            var formsAuthTiket = FormsAuthentication.Decrypt(
                                Request.Cookies[FormsAuthentication.FormsCookieName].Value);

                            if (formsAuthTiket == null)
                            {
                                FormsAuthentication.SignOut();
                                return;
                                //throw new Exception("Forms Authentication ticket is null");
                            }


                            var serializer = new JsonSerializer();
                            var uData = serializer.Deserialize<UserData>(formsAuthTiket.UserData);

                            IUsersService usersRepo =
                                    new UsersService(uData.AuthTicket.access_token, serializer);
                            
                            var user = GetCurrentUser(usersRepo);
                            //var user = new UserModel()
                            //{
                            //    Username = "******",
                            //    Email = "*****@*****.**",
                            //    Roles = new string[] { "Admin" }
                            //};

                            if (user == null)
                            {
                                FormsAuthentication.SignOut();
                                return;
                            }

                            var principal = new CustomPrincipal(user, uData);
                            HttpContext.Current.User = principal;
                            Thread.CurrentPrincipal = principal;
                        }
                        catch (Exception)
                        {
                            FormsAuthentication.SignOut();
                        }
                    }
                }
            }
        }
コード例 #2
0
        private async void SignInButton_OnClick(object sender, RoutedEventArgs e)
        {
            if (_isAuthenticationBegun)
                return;

            _isAuthenticationBegun = true;

            string email = "ionh";
            string password = "******";

            if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
                return;
            try
            {
                IAccountService accountService = new AccountService();
                var response = await accountService.Authenticate(email, password);

                if (!string.IsNullOrEmpty(response?.access_token))
                {
                    IQuestionsService questionsService = new QuestionsService(response.access_token, new JsonSerializer());
                    var question = new VariantQuestionModel()
                    {
                        Content = "Choose an answer!",
                        Enabled = true,
                        TopicId = 1,
                        Type = QuestionType.Radio,
                        UserId = 1,
                        Variants = new List<VariantModel>
                        {
                            new VariantModel() { Body = "Answer A", Correct = false},
                            new VariantModel() { Body = "Answer B", Correct = false},
                            new VariantModel() { Body = "Answer C", Correct = true},
                         }
                    };
                    var result = await questionsService.AddQuestion(question.TopicId, question);
                    MessageBox.Show(result.ToString());

                    IUsersService usersService = new UsersService(response.access_token,
                        new JsonSerializer());
                    UserModel user = await usersService.GetUser();

                    if (!user.Roles.Any(
                            r => r.Equals(RoleType.Teacher.ToString())
                                || r.Equals(RoleType.Admin.ToString())))
                        return;

                    lock (AuthenticationMonitor)
                    {
                        var mainWindow = new MainWindow(accountService);
                        Close();
                        mainWindow.Show();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                //MessageBox.Show(ex.ToString());
            }

            _isAuthenticationBegun = false;
        }