public async Task Login()
        {
            if ((!string.IsNullOrEmpty(Email) || !string.IsNullOrEmpty(Username)) && !string.IsNullOrEmpty(Password))
            {
                _userService.ApiUrl = ServerUrl;
                var upsertUser = new ApplicationUserGetRequestModel
                {
                    Email    = this.Email,
                    Password = this.Password,
                    Username = this.Username
                };
                try
                {
                    var token = await _userService.PostNoToken <TokenModel>(upsertUser, "GetToken");

                    Preferences.Set("serverUrl", ServerUrl);
                    await Application.Current.MainPage.DisplayAlert("Success", "Login successful. You can sync your notes with external server now.", "OK");

                    await SecureStorage.SetAsync("token", token.Token);
                    await LoadData();

                    ApiService.Token = token.Token;
                    Preferences.Set("loggedIn", "true");
                }
                catch { }
            }
            else
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Username/Email/Password can not be empty.", "OK");
            }
        }
Beispiel #2
0
 public IActionResult GetToken([FromBody] ApplicationUserGetRequestModel model)
 {
     try {
         var user = _userService.Get(model);
         if (user == null)
         {
             return(BadRequest(new { message = "Incorrect username/email or password." }));
         }
         var tokenHandler = new JwtSecurityTokenHandler();
         var key          = Encoding.UTF8.GetBytes(_appSettings.SecretKey);
         var claims       = new List <Claim>
         {
             new Claim(ClaimTypes.NameIdentifier, user.Username),
             new Claim(ClaimTypes.Email, user.Email),
             new Claim("UserId", user.Id.ToString())
         };
         var token = new JwtSecurityToken(
             issuer: "Notes.App",
             expires: DateTime.Now.AddMonths(3),
             claims: claims,
             signingCredentials: new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
             );
         return(Ok(new TokenModel {
             Token = tokenHandler.WriteToken(token)
         }));
     }
     catch {
         return(BadRequest());
     }
 }
Beispiel #3
0
        public async Task Login()
        {
            if ((!string.IsNullOrEmpty(Username)) && !string.IsNullOrEmpty(Password))
            {
                APIService.ApiUrl = ServerUrl;
                var upsertUser = new ApplicationUserGetRequestModel
                {
                    Password = this.Password,
                    Username = this.Username
                };
                try
                {
                    var result = await _service.Insert <TokenModel>(upsertUser);

                    if (result != null)
                    {
                        Preferences.Set("serverUrl", ServerUrl);
                        Preferences.Set("username", Username);
                        Preferences.Set("token", result.Token);
                        APIService.Token = result.Token;
                        //await LoadData();
                        await Application.Current.MainPage.DisplayAlert("", "Logged in.", "OK");
                    }
                }
                catch {
                    await Application.Current.MainPage.DisplayAlert("Error", "Something went wrong.", "OK");
                }
            }
            else
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Username/Password can not be empty.", "OK");
            }
        }
Beispiel #4
0
        public ApplicationUser Get(ApplicationUserGetRequestModel model)
        {
            ApplicationUser user = null;

            if (!string.IsNullOrEmpty(model.Username))
            {
                user = _contex.ApplicationUser.FirstOrDefault(au => au.Active == true && au.Username == model.Username);
            }
            else if (!string.IsNullOrEmpty(model.Email))
            {
                user = _contex.ApplicationUser.FirstOrDefault(au => au.Active == true && au.Email == model.Email);
            }
            if (user != null)
            {
                if (user.PasswordHash == GenerateHash(user.PasswordSalt, model.Password))
                {
                    return(user);
                }
            }
            return(null);
        }