< Summary

Information
Class: MyNet.Utilities.Authentication.IdentityHelper
Assembly: MyNet.Utilities
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Utilities/Authentication/IdentityHelper.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 13
Uncovered lines: 0
Coverable lines: 13
Total lines: 53
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetDomain(...)100%11100%
GetName(...)100%11100%
SplitIdentity(...)100%88100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Utilities/Authentication/IdentityHelper.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="IdentityHelper.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8
 9namespace MyNet.Utilities.Authentication;
 10
 11/// <summary>
 12/// Provides helper methods for parsing identity names in the format "DOMAIN\Username".
 13/// </summary>
 14public static class IdentityHelper
 15{
 16    /// <summary>
 17    /// Extracts the domain part from the identity name, assuming the format "DOMAIN\Username".
 18    /// </summary>
 19    /// <param name="identity">The identity name, usually in the format "DOMAIN\Username".</param>
 20    /// <returns>The domain part of the identity name, or an empty string when no domain is present.</returns>
 21    public static string GetDomain(string? identity)
 22    {
 4023        var (domain, _) = SplitIdentity(identity);
 4024        return domain;
 25    }
 26
 27    /// <summary>
 28    /// Extracts the username part from the identity name, assuming the format "DOMAIN\Username".
 29    /// </summary>
 30    /// <param name="identity">The identity name, usually in the format "DOMAIN\Username".</param>
 31    /// <returns>The username part of the identity name, or an empty string when unavailable.</returns>
 32    public static string GetName(string? identity)
 33    {
 4034        var (_, name) = SplitIdentity(identity);
 4035        return name;
 36    }
 37
 38    private static (string Domain, string Name) SplitIdentity(string? identity)
 39    {
 8040        if (string.IsNullOrWhiteSpace(identity))
 2241            return (string.Empty, string.Empty);
 42
 5843        var separatorIndex = identity.AsSpan().IndexOf('\\');
 44
 5845        return separatorIndex switch
 5846        {
 1647            < 0 => (string.Empty, identity),
 648            0 => (string.Empty, identity[1..]),
 3649            _ => separatorIndex == identity.Length - 1 ? (identity[..separatorIndex], string.Empty) : (identity[..separa
 5850        };
 51    }
 52}
 53