示例#1
0
        public async Task <LoginResponseModel> GetLoginResponse(CustomerModel customer)
        {
            var customerDetails = await(from customerTable in _customerRepository.Table
                                        join customerRoleMapping in _customerCustomerRoleRepository.Table on customerTable.Id equals customerRoleMapping.CustomerId
                                        join customerRole in _customerRoleRepository.Table on customerRoleMapping.CustomerRoleId equals customerRole.Id
                                        where customerTable.Id == customer.Id
                                        select new
            {
                CustomerId       = customerTable.Id,
                Username         = customerTable.UserName,
                CustomerTypeId   = customerRoleMapping.CustomerRoleId,
                CustomerTypeName = customerRole.Name,
            }).ToListAsync();

            if (customerDetails == null)
            {
                return(null);
            }

            var tokenModel = new CustomerLoginTokenModel();

            foreach (var item in customerDetails)
            {
                tokenModel.CustomerId = item.CustomerId;
                tokenModel.UserName   = item.Username;
                tokenModel.CustomerRoleIds.Add(item.CustomerTypeId);
                tokenModel.CustomerRoleNames.Add(item.CustomerTypeName);
            }
            return(await GenerateToken(tokenModel));
        }
示例#2
0
        public static string GenerateToken(CustomerLoginTokenModel tokenModel)
        {
            var IssuedOn = DateTime.Now;

            try
            {
                string randomnumber =
                    string.Join(":", new string[]
                {
                    Convert.ToString(tokenModel.CustomerId),
                    GetUniqueKey(),
                    string.Join(",", tokenModel.CustomerRoleIds.ToArray()),
                    Convert.ToString(IssuedOn.Ticks)
                });

                return(EncryptionLibrary.EncryptText(randomnumber));
            }
            catch (Exception)
            {
                throw;
            }
        }
示例#3
0
        public async Task <LoginResponseModel> GenerateToken(CustomerLoginTokenModel tokenModel)
        {
            try
            {
                var token = await GetTokenByCustomerId(tokenModel.CustomerId);

                if (token != null)
                {
                    await DeleteToken(token);
                }

                var newToken = new TokenManager
                {
                    Id         = 0,
                    TokenKey   = KeyGenerator.GenerateToken(tokenModel),
                    IssuedOn   = DateTime.Now,
                    ExpiresOn  = DateTime.Now.AddMinutes(30),
                    CustomerId = tokenModel.CustomerId,
                    CreatedBy  = tokenModel.CustomerId,
                    CreatedOn  = DateTime.Now
                };

                await InsertToken(newToken);

                return(new LoginResponseModel
                {
                    UserName = tokenModel.UserName,
                    Token = newToken.TokenKey,
                    CustomerTypeIds = string.Join(",", tokenModel.CustomerRoleIds.ToArray()),
                    IsAdmin = tokenModel.CustomerRoleIds.Contains((int)CustomerRoleEnum.Admin)
                });
            }
            catch (Exception)
            {
                throw;
            }
        }