public async Task <IActionResult> Token(StringModel <LoginDto> StringModel)
        {
            try
            {
                LoginDto model = AESEncryptDecrypt <LoginDto> .DecryptStringAES(StringModel.Model);

                byte[] ecodedString = Convert.FromBase64String(model.Password);
                string password     = Encoding.UTF8.GetString(ecodedString);

                Microsoft.AspNetCore.Identity.SignInResult result = await _signInManager.PasswordSignInAsync(model.Username, password, false, false);

                if (result.Succeeded)
                {
                    AppIdentityUser appUser = _userManager.Users.SingleOrDefault(r => r.UserName == model.Username);
                    if (appUser == null)
                    {
                        return(Ok(new APIResult <Guid> {
                            Message = Messages.NoRecord, IsSuccess = false
                        }));
                    }

                    IList <string> appUserRoles;
                    appUserRoles = await _userManager.GetRolesAsync(appUser);

                    Service.DTOs.UserCardDto user = await _userService.GetUserByIdentityID(Guid.Parse(appUser.Id));

                    string token = TokenBuilder.CreateJsonWebToken(appUser.UserName, appUserRoles, "https://www.tulparyazilim.com.tr", "https://www.tulparyazilim.com.tr", Guid.NewGuid(), DateTime.UtcNow.AddDays(30));

                    var data = new TokenDto
                    {
                        ValidTo  = DateTime.UtcNow.AddDays(30),
                        Value    = token,
                        Roles    = string.Join(',', appUserRoles),
                        Username = appUser.UserName,
                        Email    = appUser.Email
                    };

                    string str            = Newtonsoft.Json.JsonConvert.SerializeObject(data);
                    string base64Data     = Convert.ToBase64String(Encoding.Default.GetBytes(str));
                    string encryptredData = AESEncryptDecrypt <string> .EncryptStringAES(base64Data);

                    return(Ok(new APIResult <string>
                    {
                        Message = Messages.Ok,
                        Data = encryptredData,
                        IsSuccess = true
                    }));
                }
                return(Ok(new APIResult <Guid> {
                    Message = Messages.NoRecord, IsSuccess = false
                }));
            }
            catch (Exception ex)
            {
                return(Ok(new APIResult <Guid> {
                    Message = ex.ToString(), IsSuccess = false
                }));
            }
        }
Exemple #2
0
        public async Task <IActionResult> Register(StringModel <RegisterDto> StringModel)
        {
            try
            {
                RegisterDto model = AESEncryptDecrypt <RegisterDto> .DecryptStringAES(StringModel.Model);

                byte[] ecodedString = Convert.FromBase64String(model.Password);
                string password     = Encoding.UTF8.GetString(ecodedString);

                AppIdentityUser identityUser = new AppIdentityUser
                {
                    UserName    = model.Username,
                    Email       = model.Email,
                    PhoneNumber = model.Phone
                };

                IdentityResult result = await _userManager.CreateAsync(identityUser, password);

                if (result.Succeeded)
                {
                    if (model.Roles != null && model.Roles.Any())
                    {
                        IdentityResult rolesResult = await _userManager.AddToRolesAsync(identityUser, model.Roles);
                    }

                    APIResult <Guid> userResult = await service.Register(model, Guid.Parse(identityUser.Id));

                    if (userResult.IsSuccess)
                    {
                        return(Ok(userResult));
                    }
                    else
                    {
                        await _userManager.DeleteAsync(identityUser);

                        return(Ok(userResult));
                    }
                }
                else
                {
                    return(Ok(new APIResult <List <string> > {
                        Message = Messages.Error, Data = result.Errors.Select(x => x.Description).ToList(), IsSuccess = false
                    }));
                }
            }
            catch (Exception ex)
            {
                return(Ok(new APIResult <List <string> > {
                    Message = Messages.Error, Data = new List <string> {
                        ex.ToString()
                    }, IsSuccess = false
                }));
            }
        }
Exemple #3
0
        /// <summary>
        /// Update User
        /// </summary>
        /// <param name="StringModel"></param>
        /// <returns></returns>
        public override async Task <IActionResult> Update(StringModel <UserUpdateDto> StringModel)
        {
            try
            {
                UserUpdateDto userUpdateDto = AESEncryptDecrypt <UserUpdateDto> .DecryptStringAES(StringModel.Model);

                APIResult <Guid> userUpdateResult = await service.Update(userUpdateDto, GetUserId <Guid>());

                if (userUpdateResult.Message != Messages.Ok)
                {
                    return(Ok(userUpdateResult));
                }

                AppIdentityUser identityUser = await _userManager.FindByIdAsync(userUpdateDto.IdentityUserID);

                IList <string> identityUserRoles = await _userManager.GetRolesAsync(identityUser);

                IdentityResult removeRolesResult = await _userManager.RemoveFromRolesAsync(identityUser, identityUserRoles);

                if (!removeRolesResult.Succeeded)
                {
                    return(Ok(new APIResult <List <string> > {
                        Message = Messages.Error, Data = removeRolesResult.Errors.Select(x => x.Description).ToList(), IsSuccess = false
                    }));
                }
                IdentityResult addRolesResult = await _userManager.AddToRolesAsync(identityUser, userUpdateDto.Roles);

                if (!addRolesResult.Succeeded)
                {
                    return(Ok(new APIResult <List <string> > {
                        Message = Messages.Error, Data = addRolesResult.Errors.Select(x => x.Description).ToList(), IsSuccess = false
                    }));
                }

                return(Ok(userUpdateResult));
            }
            catch (Exception ex)
            {
                return(Ok(new APIResult <List <string> > {
                    Data = new List <string> {
                        ex.ToString()
                    }, Message = Messages.Error, IsSuccess = false
                }));
            }
        }
        public IActionResult Send(StringModel <NotificationModel> StringModel)
        {
            try
            {
                NotificationModel notificationModel = AESEncryptDecrypt <NotificationModel> .DecryptStringAES(StringModel.Model);

                var sendResult = oneSignalService.SendToNotification(notificationModel.Title, notificationModel.Message, notificationModel.Users, notificationModel.SendDate);
                if (!sendResult.IsSuccess)
                {
                    return(Ok(sendResult));
                }
                return(Ok(new APIResult <string> {
                    IsSuccess = true
                }));
            }
            catch (Exception ex)
            {
                return(Ok(new APIResult <string> {
                    Message = ex.ToString(), IsSuccess = false
                }));
            }
        }