Exemplo n.º 1
0
 public static void BorrowReader(SqlDataReader reader, List <ApiSession> list)
 {
     while (reader.Read())
     {
         var model = new ApiSession();
         model.CreatedAt   = reader.GetDateTimeOffset(reader.GetOrdinal("CreatedAt"));
         model.BasicToken  = reader.GetGuid(reader.GetOrdinal("BasicToken"));
         model.BearerToken = reader.GetString(reader.GetOrdinal("BearerToken"));
         model.TenantId    = reader.GetInt32(reader.GetOrdinal("TenantId"));
         model.PatronId    = reader.GetInt32(reader.GetOrdinal("PatronId"));
         model.SsoPatronId = reader.GetInt32(reader.GetOrdinal("SsoPatronId"));
         if (!reader.IsDBNull(reader.GetOrdinal("IPAddress")))
         {
             model.IPAddress = reader.GetString(reader.GetOrdinal("IPAddress"));
         }
         if (!reader.IsDBNull(reader.GetOrdinal("UserAgent")))
         {
             model.UserAgent = reader.GetString(reader.GetOrdinal("UserAgent"));
         }
         if (!reader.IsDBNull(reader.GetOrdinal("PassKey")))
         {
             model.PassKey = reader.GetString(reader.GetOrdinal("PassKey"));
         }
         if (!reader.IsDBNull(reader.GetOrdinal("LinesOfBusiness")))
         {
             model.LinesOfBusiness = reader.GetString(reader.GetOrdinal("LinesOfBusiness"));
         }
         list.Add(model);
     }
 }
Exemplo n.º 2
0
        public static ApiSession Get(string bearerToken)
        {
            ApiSession model = new ApiSession();

            if (!String.IsNullOrWhiteSpace(bearerToken))
            {
                string schema   = eXtensibleConfig.Zone.Equals("production", StringComparison.OrdinalIgnoreCase) ? DateTime.Today.ToString("MMM").ToLower() : "log";
                var    settings = ConfigurationProvider.ConnectionStrings[eXtensibleWebApiConfig.SqlConnectionKey];
                if (settings != null && !String.IsNullOrWhiteSpace(settings.ConnectionString))
                {
                    List <ApiSession> list = new List <ApiSession>();
                    try
                    {
                        using (SqlConnection cn = new SqlConnection(settings.ConnectionString))
                        {
                            cn.Open();
                            using (SqlCommand cmd = cn.CreateCommand())
                            {
                                const string BearerTokenParamName = "@bearertoken";

                                string sql = "select [Id], [CreatedAt], [BasicToken], [BearerToken], [TenantId], [PatronId], [SsoPatronId]," +
                                             " [IPAddress], [UserAgent], [PassKey], [LinesOfBusiness] from [" + schema + "].[Session] " +
                                             " where [BearerToken] = " + BearerTokenParamName;

                                cmd.CommandText = sql;

                                using (SqlDataReader reader = cmd.ExecuteReader())
                                {
                                    BorrowReader(reader, list);
                                }

                                if (list.Count > 0)
                                {
                                    model = list[0];
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        var message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                        var props   = eXtensibleConfig.GetProperties();
                        EventWriter.WriteError(message, SeverityType.Error, "DataAccess", props);
                    }
                }
            }


            return(model);
        }