.NET and Windows Auth: “The trust relationship between this workstation and the primary domain failed.”

So you inherit a legacy project that uses windows authentication, and you’re trying to use the “User.IsInRole(string s)” method to determine a user’s role and you keep getting a “SystemException” with message: “The trust relationship between this workstation and the primary domain failed.”

This seems to be a pretty common problem judging the amount of articles on the web discussing this “SystemException”. Below you will find the steps that I had to take to resolve my issue. Remember, there are a ton of different issues that could be the cause; my steps may or may not work for you depending on what your actual issue is.

Project’s Web.config with SystemException being thrown:

 <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet" />
        <add name="HttpPost" />
      </protocols>
    </webServices>
    <authentication mode="Windows" />
    <authorization>
      <allow verbs="OPTIONS" users="*" />
      <deny users="?" />
    </authorization>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
  </system.web>

Project’s Web.config after role manager fix that solves SystemException issue:

<system.web>
    <webServices>
      <protocols>
        <add name="HttpGet" />
        <add name="HttpPost" />
      </protocols>
    </webServices>
    <authentication mode="Windows" />
    <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider">
      <providers>
        <clear />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
    <authorization>
      <allow verbs="OPTIONS" users="*" />
      <deny users="?" />
    </authorization>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
  </system.web>

As you can see, the “roleManager” tag was missing in the Web.config.

Go ahead and try to see if that resolves your issue.

Hibernate: Creating and Populating “created_time” and “last_mod_time” Columns on SQL Tables

When writing REST services or web applications, having “created_time” or “last_mod_time” columns on a per table basis is a super common requirement. The preferable way to accomplish this is to generally use a “SQL trigger”.

One of the problems with the trigger based approach is that some ORMs don’t play nice with triggers. Additionally, they often require you to refetch the entity. There are many ways to handle this problem:

  • Define the “created_time” or “last_mod_time” using only the ORM. Do not use triggers.
  • Use a hybrid approach, have ORM generated times and “SQL trigger” generated times, resulting in “4” columns instead of “2” columns. 
  • Have the ORM populate values and then design the trigger to not override non-null values.
  • Accept the extra cost of refetching the entity.

This article will focus on using hibernate to generate the “created_time” and “last_mod_time” fields. Creating or integrating a “SQL trigger” for this code will be an exercise left to the reader.

First, we define our system times collection:

import com.google.common.base.MoreObjects;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.time.Instant;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@MappedSuperclass
public class SystemTypeCollection {
    @Basic
    @Column(name = "system_insert_time", nullable = false)
    private Instant systemInsertTime;
    @Basic
    @Column(name = "system_update_time", nullable = false)
    private Instant systemUpdateTime;
    @Basic
    @Column(name = "system_insert_type", nullable = false, length = -1)
    private String systemInsertType;
    @Basic
    @Column(name = "system_update_type", nullable = false, length = -1)
    private String systemUpdateType;

    @PrePersist
    public void prePersist() {
        systemInsertTime = MoreObjects.firstNonNull(systemInsertTime, Instant.now());
        systemUpdateTime = MoreObjects.firstNonNull(systemUpdateTime, Instant.now());
        systemInsertType = MoreObjects.firstNonNull(systemInsertType, SystemTypes.System.name());
        systemUpdateType = MoreObjects.firstNonNull(systemUpdateType, SystemTypes.System.name());
    }

    @PreUpdate
    public void preUpdate() {
        systemUpdateTime = Instant.now();
        systemUpdateType = MoreObjects.firstNonNull(systemUpdateType, SystemTypes.System.name());
    }
}

Second, we define our System enum:

public enum SystemTypes {
    System
}

Third, we use our previous class in our entities:

import lombok.*;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.UUID;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "sample_line_entity", schema = "sample", catalog = "postgres")
public class SampleLineEntity extends SystemTypeCollection implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "id", nullable = false)
    private UUID id;
}

Finally, we create the “SampleLineEntity” with SQL:

create table if not exists sample_line_entity (
    id uuid not null default uuid_generate_v4(),
    system_insert_time timestamp not null,
    system_update_time timestamp not null,
    system_insert_type varchar not null,
    system_update_type varchar not null,
    constraint sample_line_entity_pkey primary key (id),
);

You should be good to go. The “SampleLineEntity”, on save or update, should have the “system_*” fields populated. As said before, adding a trigger will be a an exercise left to the reader.

Midwest Software Engineering Interview: Phone Interview

There seems to be a lot of literature available on the technical interview process for Silicon Valley tech companies but not nearly as much for companies in the Midwest. I wanted to go ahead and post a sample phone screen that I conducted to give my readers a feel for what type of questions they should be expecting when interviewing for companies in the Midwest.

I always, when interviewing candidates for companies, keep in mind their compensation structure, what level of software engineering knowledge they need, and how senior of a candidate they’re expecting. The level of rigorousness of the technical interview will change depending on these factors.

Below, you will find a sample phone screen that I conducted.

Computer Science:
Q: What is a dynamic array in C#?
Q: What is the time complexity of inserting into a dynamic array?
Q: Tell me about hash tables? How are they implemented? What is the time complexity of a “put” or “insert” operation?
Q: What are the searching algorithms in computer science? What are the time complexity of the algorithms?
Q: Tell me the differences between unclustered and clustered indexes?
Q: Tell me the three pillars of OOP?
Q: Talk to me regarding dependency injection and inversion of control?
Q: Have you heard of SOLID acronym?
Q: What are 4 SQL operations?
Q: Sorting algorithms?

I’m not looking for the correct answer to every single question but really just trying to get a feel for the depth of their knowledge.

I hope that helps to clear up what some Midwest companies are looking for.

Graphics Card: White Blinking Light After Moving PC

So, you finally got the opportunity to move your PC. After moving your PC, the graphics card has a white light blinking. You try to plug in the monitors to the graphics card but the monitors display “No Signal Found”. You’re thinking, “Man, another headache to deal with! I hope that I won’t have to take the PC apart..arghh.” You frantically search Google, hoping for a quick software solution, but deep down, you know and understand that your going to have to take the PC apart.

You Google “graphics card white light blinking”, “pc graphic card white light blinking”, …, …, “after pc moving graphics card white light blinking” to no avail. But finally, you run into my article.

I ran into this exact same problem. Make sure you choose a good place to do this where there isn’t much static electricity; a place with fuzzy carpeting isn’t a good place to do this.

Here’s what solved my issue:

  1. Power off your comupter
  2. Unplug monitors
  3. Hit “off” switch on PSU and unplug power cable to ensure the PSU doesn’t have any power.
  4. Ground yourself touching case or wearing anti static wrist wrap
  5. Unscrew computer case
  6. Firmly push in all connections to PSU and graphics card ensuring those connections are secure.
  7. Plug back in and see if everything works.
  8. If everything works, voila, you’re done. If not, we’re going to have to do more steps. We only did those initial steps to minimize the probability of accidentally breaking something.
  9. Turn off computer, hit ‘off’ button on PSU, unplug power sources, unplug monitors, and ground yourself again.
  10. Reseat graphics card while making sure the connections are plugged in securely.
  11. Test to see if everything works again. If not, try to reseat the RAM and if that doesn’t work, then your going to have to do some more troubleshooting that falls outside the scope of this article.

Leave a comment below and let me know if that helped you.