Snippset

Snippset Feed

...see more

This solution demonstrates how to retrieve the password expiration date of a user account in Active Directory using PowerShell. It uses the Get-ADUser cmdlet from the Active Directory module and queries the msDS-UserPasswordExpiryTimeComputed property, which holds the computed expiration date in FILETIME format.

If querying by -Identity returns an error such as "Cannot find an object with identity," switching to a -Filter approach with the SamAccountName is recommended. Also, ensure that the Active Directory module is imported, the domain context is correct, and the executing user has appropriate permissions.

# Import the Active Directory module if not already loaded
Import-Module ActiveDirectory

# Replace 'username' with the actual SamAccountName of the user
$user = Get-ADUser -Filter {SamAccountName -eq "username"} -Properties msDS-UserPasswordExpiryTimeComputed

# Convert the FILETIME to a readable DateTime object
$passwordExpiry = if ($user."msDS-UserPasswordExpiryTimeComputed") {
    [datetime]::FromFileTime($user."msDS-UserPasswordExpiryTimeComputed")
} else {
    "Password does not expire or no expiration set."
}

# Output the result
[PSCustomObject]@{
    UserName       = $user.SamAccountName
    PasswordExpiry = $passwordExpiry
}

Key Points:

  • Use -Filter with SamAccountName to avoid identity resolution issues.
  • The property msDS-UserPasswordExpiryTimeComputed returns the expiration time as FILETIME.
  • Convert FILETIME to DateTime for human-readable output.
  • Confirm the environment context and permissions to avoid access errors.
C# by Myles
...see more

When working with collections in C#, it's common to extract and concatenate specific property values for display or logging purposes. For instance, if you have a list of objects and want to join the values of a particular string property, you can use LINQ in combination with string.Join.

Scenario

You have a list of objects, each with a Name property, and you want to create a single comma-separated string of all names.

Solution

Use LINQ to project the property and string.Join to concatenate the results.

var names = string.Join(", ", items.Select(i => i.Name));

Notes

  • items is a List<T> where T has a Name property.
  • Select(i => i.Name) projects the desired property Name from each Item Item.
  • string.Join concatenates the values with a defined separator (e.g., ", " in this case).
  • To avoid nulls, you can add .Where(x => !string.IsNullOrEmpty(x.Name)).

This method is clean, efficient, and readable—ideal for transforming object data into user-friendly output or log formats.

...see more

DelegatingHandlers can be tested in isolation:

  1. Mock the Inner Handler:

    var mockHandler = new Mock<HttpMessageHandler>();
    mockHandler.Protected()
    .Setup<Task<HttpResponseMessage>>("SendAsync", ...)
    .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));
  2. Inject into DelegatingHandler:

    var customHandler = new CustomHandler
    {
    InnerHandler = mockHandler.Object
    };
  3. Create HttpClient:

    var client = new HttpClient(customHandler);

This approach allows for unit testing the DelegatingHandler's logic without making actual HTTP calls.

...see more

This video tutorial provides a hands-on introduction to jsTree. The walkthrough covers the setup process, basic configuration, and demonstrates how to create a simple tree structure using the library. It’s aimed at developers looking for a quick start with jsTree and practical examples of how it integrates into HTML pages.

Highlights from the video:

  • Download and setup of jsTree
  • Adding a basic tree structure with static data
  • Using plugins like checkbox and search
  • Tips for integration and customization

While the video is specific to jsTree, the concepts are transferable to similar tree view libraries.

...see more

Multiple DelegatingHandlers can be chained to create a pipeline:

services.AddTransient<LoggingHandler>();
services.AddTransient<AuthenticationHandler>();
services.AddHttpClient("ChainedClient")
.AddHttpMessageHandler<LoggingHandler>()
.AddHttpMessageHandler<AuthenticationHandler>();

In this setup, LoggingHandler processes the request first, followed by AuthenticationHandler. The order of registration determines the sequence of execution.

Windows by Burton
...see more

After enabling Stereo Mix, configure your playback devices:

  1. In the Playback tab of the Sound Control Panel, right-click your primary audio device and select Properties.
  2. Navigate to the Listen tab.
  3. Check the box labeled Listen to this device.
  4. From the dropdown menu, select Stereo Mix as the playback device.
  5. Click Apply to confirm the changes.

This configuration routes audio through both selected devices simultaneously.

.NET by Jerry
...see more

C#'s async/await pattern simplifies asynchronous programming, but integrating it into console applications poses a challenge. The traditional static void Main() method can't be marked as async, leading to compiler errors when attempting to use await directly.

Workaround Strategies:

  • Separate Async Method: Encapsulate asynchronous operations within a separate method marked as async. Then, invoke this method from Main() using .GetAwaiter().GetResult() to execute it synchronously. This approach ensures exceptions are unwrapped properly, avoiding the AggregateException that occurs with .Result or .Wait().

  • Async Main (C# 7.1 and Later): Starting with C# 7.1, you can define the entry point as static async Task Main(), allowing the use of await directly within Main(). This modernizes the approach and simplifies asynchronous code execution in console applications.

For a detailed explanation and code examples see Async/await in a console application.

...see more

To create a custom DelegatingHandler:

  1. Inherit from DelegatingHandler:

    public class CustomHandler : DelegatingHandler
    {
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
    // Pre-processing logic
    var response = await base.SendAsync(request, cancellationToken);
    // Post-processing logic
    return response;
    }
    }
  2. Register the Handler:
    In ASP.NET Core, register the handler using IHttpClientFactory:

    services.AddTransient<CustomHandler>();
    services.AddHttpClient("NamedClient")
    .AddHttpMessageHandler<CustomHandler>();
...see more

Summary:
This video demonstrates how to generate a TreeView structure using data directly from a SQL Server database in an MVC 4 application. The example uses recursive methods to fetch hierarchical data from a parent-child table (such as a category structure). The TreeView is rendered using a combination of Razor and recursive HTML helpers.

Key Steps:

  • Create a SQL table with ID and ParentID fields.
  • Use Entity Framework to fetch data into a recursive model.
  • Write recursive logic in the controller to build the tree.
  • Use a partial view or helper method to render nested HTML in the view.

Best For:
Developers working with older ASP.NET MVC versions who need to generate TreeViews from database-driven content, particularly with dynamic data structures.

Add to Set
  • .NET
  • Agile
  • AI
  • ASP.NET Core
  • Azure
  • C#
  • Cloud Computing
  • CSS
  • EF Core
  • HTML
  • JavaScript
  • Microsoft Entra
  • PowerShell
  • Quotes
  • React
  • Security
  • Software Development
  • SQL References
  • Technologies
  • Testing
  • Visual Studio
  • Windows