コード例 #1
0
        public async Task<ActionResult> ResetPassword(ResetPassword resetingPw)
        {
            // get bearer token for reset password post access
            var url = string.Format("{0}/token", SecureWebApiAddress);
            HttpClient client = new HttpClient();

            var xFormData = string.Format("grant_type=password&username={0}&password={1}", Email, Password);
            var content = new StringContent(xFormData, Encoding.UTF8, "application/x-www-form-urlencoded");
            HttpResponseMessage response = null;
            response = await client.PostAsync(new Uri(url), content);

            if (!response.IsSuccessStatusCode) return View(resetingPw);

            var responseContent = await response.Content.ReadAsStringAsync();
            var jsonToken = JObject.Parse(responseContent);
            var token = new AuthenticationToken
            {
                AccessToken = jsonToken["access_token"].ToString(),
                TokenType = jsonToken["token_type"].ToString(),
                ExpiresIn = Convert.ToInt64(jsonToken["expires_in"]),
                Expires = DateTime.UtcNow.AddSeconds(Convert.ToDouble(jsonToken["expires_in"])),
                UserId = jsonToken["userId"].ToString()
            };
            // post reset password
            resetingPw.Code = HttpUtility.UrlEncode(resetingPw.Code);
            url = $"{WebApiAddress}/api/account/resetpassword";
            client = new HttpClient();
            client.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue(token.TokenType, token.AccessToken);

            var json = JsonConvert.SerializeObject(resetingPw);
            content = new StringContent(json, Encoding.UTF8, "application/json");
            
            response = await client.PostAsync(new Uri(url), content);

            switch (response.StatusCode)
            {
                case HttpStatusCode.OK:
                    ViewBag.response = response;
                    return RedirectToAction("ResetPasswordConfirmation", "Home");
                case HttpStatusCode.NotFound:
                    ViewBag.response = response;
                    return RedirectToAction("ResetPasswordConfirmation", "Home");
                case HttpStatusCode.BadRequest:
                    ViewBag.response = response;
                    return View(resetingPw);
                default:
                    ViewBag.response = response;
                    return View();
            }
            //return View(resetingPw);
        }
コード例 #2
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.MyFriends);

            preferences = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
            token = JsonConvert.DeserializeObject<AuthenticationToken>(preferences.GetString("token", null));

            mAddFriendButton = FindViewById<ImageButton>(Resource.Id.addFriendImageButton);
            mMyFriendsList = FindViewById<ListView>(Resource.Id.MyFriendsListView);
            mItems = new List<FriendListItem>();
            RegisterForContextMenu(mMyFriendsList);
            GetMyFriends();

            mAddFriendButton.Click += (object sender, EventArgs e) =>
            {
                StartActivityForResult(typeof(FindFriendToAddActivity), 2);
            };

        }
コード例 #3
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.NewMessage);

            preferences = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
            token = JsonConvert.DeserializeObject<AuthenticationToken>(preferences.GetString("token", null));

            mTimeTextView = FindViewById<TextView>(Resource.Id.timeTextView);
            mStartPlaceEditText = FindViewById<EditText>(Resource.Id.startPlaceEditText);
            mEndPlaceEditText = FindViewById<EditText>(Resource.Id.endPlaceEditText);
            mAddButton = FindViewById<Button>(Resource.Id.addButton);

            mTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
            mTimeTextView.Text = $"Czas: {mTime.ToString("HH:mm")}";

            mTimeTextView.Click += (object sender, EventArgs e) =>
            {
                var timePickerFragment = new TimePickerDialogFragment(this, mTime, this);
                timePickerFragment.Show(FragmentManager, null);
            };

            mAddButton.Click += async (object sender, EventArgs e) =>
            {   //sprawdzenie poprawnoœci wprowadzonych przez u¿ytkownika danych
                if (!mStartPlaceEditText.Text.Equals("") && !mEndPlaceEditText.Text.Equals(""))
                {
                    mStartPlace = mStartPlaceEditText.Text;
                    mEndPlace = mEndPlaceEditText.Text;
                    var traveler = JsonConvert.DeserializeObject<Traveler>(preferences.GetString("traveler", null));
                    if (traveler == null || token == null)
                    {
                        Toast.MakeText(this, GetString(Resource.String.LoginTokenOrTravelerFailure), ToastLength.Long).Show();
                        Finish();
                        StartActivity(typeof(LoginActivity));
                    }
                    else
                    {
                        //utworzenie nowego obiektu - wiadomoœci
                        newMessage = new Information
                        {
                            TravelerId = traveler.Id,
                            StartPlace = mStartPlace,
                            EndPlace = mEndPlace,
                            Time = mTime
                        };
                        //sprawdzenie, czy œrodowisko jest po³¹czone z sieci¹ Internet
                        var connectivityManager = (ConnectivityManager) GetSystemService(ConnectivityService);
                        var activeNetworkInfo = connectivityManager.ActiveNetworkInfo;
                        if (activeNetworkInfo == null || !activeNetworkInfo.IsConnected)
                        {
                            Toast.MakeText(this, GetString(Resource.String.NoConnectionInfo), ToastLength.Long).Show();
                            return;
                        }
                        //wywo³anie okna dialogowego progresu
                        var loadingMessage = ProgressDialog.Show(this, GetString(Resource.String.SendingMessageTitle),
                            GetString(Resource.String.SendingMessageContent), true, false);

                        //wywo³anie metody obs³uguj¹cej ¿¹danie http i oczekiwanie na wiadomoœæ
                        var apiMessage = await SaveMyMessage(newMessage);
                        loadingMessage.Dismiss();
                        if (apiMessage.IsSuccessStatusCode)
                        {
                            //ustawienie pozytywnego rezultatu operacji i zakoñczenie aktywnoœci
                            SetResult(Result.Ok);
                        }
                        Finish();
                    }
                }
                else Toast.MakeText(this, GetString(Resource.String.NewMessageNoDataInfo), ToastLength.Long).Show();
            };
        }
コード例 #4
0
ファイル: LoginActivity.cs プロジェクト: piotreqm/JedzmyRazem
        private async void MLoginButton_Click(object sender, EventArgs e)
        {
            if (!mEmailField.Text.Equals("") && !mPassField.Text.Equals(""))
            {
                email = mEmailField.Text;
                password = mPassField.Text;

                var url = GetString(Resource.String.SecureApiLink) + "/token";
                client = new HttpClient();

                var loadingMessage = ProgressDialog.Show(this, GetString(Resource.String.LoginProgressTitle),
                    GetString(Resource.String.LoginProgressContent), true, false);
                var xFormData = $"grant_type=password&username={email}&password={password}";
                var content = new StringContent(xFormData, Encoding.UTF8, "application/x-www-form-urlencoded");

                var connectivityManager = (ConnectivityManager) GetSystemService(ConnectivityService);
                var activeNetworkInfo = connectivityManager.ActiveNetworkInfo;
                if (activeNetworkInfo == null || !activeNetworkInfo.IsConnected)
                {
                    loadingMessage.Dismiss();
                    Toast.MakeText(this, GetString(Resource.String.NoConnectionInfo),ToastLength.Long).Show();
                    return;
                } 
                
                var response = await client.PostAsync(new Uri(url), content);
                if (response.IsSuccessStatusCode)
                {
                    var responseContent = await response.Content.ReadAsStringAsync();
                    var jsonToken = JObject.Parse(responseContent);
                    token = new AuthenticationToken
                    {
                        AccessToken = jsonToken["access_token"].ToString(),
                        TokenType = jsonToken["token_type"].ToString(),
                        ExpiresIn = Convert.ToInt64(jsonToken["expires_in"]),
                        Expires = DateTime.UtcNow.AddSeconds(Convert.ToDouble(jsonToken["expires_in"])),
                        UserId = jsonToken["userId"].ToString()
                    };
                    editor.PutString("token", JsonConvert.SerializeObject(token));
                    editor.Apply();

                    url = string.Format(GetString(Resource.String.ApiLink)
                                        + "/api/traveler/gettravelerbyuserid?userid={0}", token.UserId);
                    client.DefaultRequestHeaders.Authorization =
                        new AuthenticationHeaderValue(token.TokenType, token.AccessToken);
                    response = await client.GetAsync(new Uri(url));
                    loadingMessage.Dismiss();
                    if (response.IsSuccessStatusCode)
                    {
                        responseContent = await response.Content.ReadAsStringAsync();
                        editor.PutString("traveler", responseContent);
                        editor.Apply();
                        StartActivityForResult(typeof (MainActivity), 11);
                    }
                    else
                        Toast.MakeText(this, GetString(Resource.String.LoginTokenOrTravelerFailure), ToastLength.Long)
                            .Show();
                }
                else
                {
                    loadingMessage.Dismiss();
                    Toast.MakeText(this, GetString(Resource.String.LoginTokenOrTravelerFailure),ToastLength.Long).Show();
                }
            }
            else Toast.MakeText(this, GetString(Resource.String.LoginFieldsNoDataInfo), ToastLength.Long).Show();
        }