Skip to content

Commit

Permalink
Use C# 10 implicit usings and file scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Nov 9, 2021
1 parent 3e7840c commit 7b641b0
Show file tree
Hide file tree
Showing 497 changed files with 33,524 additions and 34,469 deletions.
25 changes: 23 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ indent_size = 4
# Xml project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
indent_size = 2
dotnet_style_allow_multiple_blank_lines_experimental=false:silent

# Xml config files
[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}]
Expand Down Expand Up @@ -45,10 +46,21 @@ dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion

# Naming style
dotnet_naming_rule.private_members_with_underscore.symbols = private_fields
dotnet_naming_rule.private_members_with_underscore.style = prefix_underscore
dotnet_naming_rule.private_members_with_underscore.severity = suggestion

dotnet_naming_symbols.private_fields.applicable_kinds = field
dotnet_naming_symbols.private_fields.applicable_accessibilities = private

dotnet_naming_style.prefix_underscore.capitalization = camel_case
dotnet_naming_style.prefix_underscore.required_prefix = _

# CSharp code style settings:
[*.cs]
# Prefer "var" everywhere
csharp_style_var_for_built_in_types = false:suggestion
csharp_style_var_for_built_in_types = false:silent
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = true:suggestion

Expand All @@ -68,11 +80,20 @@ csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
csharp_style_namespace_declarations=file_scoped:silent
csharp_style_prefer_range_operator=false:suggestion

# Newline settings
csharp_new_line_before_open_brace = false:error
csharp_new_line_before_else = false:error
csharp_new_line_before_catch = false:error
csharp_new_line_before_finally = false:error
csharp_new_line_before_members_in_object_initializers = false:error
csharp_new_line_before_members_in_anonymous_types = false:error
csharp_new_line_before_members_in_anonymous_types = false:error

# CS1701: Assuming assembly reference matches identity
dotnet_diagnostic.CS1701.severity = none

# CS1702: Assuming assembly reference matches identity
dotnet_diagnostic.CS1702.severity = none
csharp_prefer_braces=when_multiline:silent
1 change: 1 addition & 0 deletions build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Product>Exceptionless</Product>
<MinVerSkip Condition="'$(Configuration)' == 'Debug'">true</MinVerSkip>
<MinVerTagPrefix>v</MinVerTagPrefix>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,62 @@
using System.DirectoryServices;
using Exceptionless.Core.Configuration;

namespace Exceptionless.Core.Authentication {
public class ActiveDirectoryLoginProvider : IDomainLoginProvider {
private const string AD_EMAIL = "mail";
private const string AD_FIRSTNAME = "givenName";
private const string AD_LASTNAME = "sn";
private const string AD_DISTINGUISHEDNAME = "distinguishedName";
private const string AD_USERNAME = "sAMAccountName";
namespace Exceptionless.Core.Authentication;

private readonly AuthOptions _authOptions;
public class ActiveDirectoryLoginProvider : IDomainLoginProvider {
private const string AD_EMAIL = "mail";
private const string AD_FIRSTNAME = "givenName";
private const string AD_LASTNAME = "sn";
private const string AD_DISTINGUISHEDNAME = "distinguishedName";
private const string AD_USERNAME = "sAMAccountName";

public ActiveDirectoryLoginProvider(AuthOptions authOptions) {
_authOptions = authOptions;
}
private readonly AuthOptions _authOptions;

public bool Login(string username, string password) {
using (var de = new DirectoryEntry(_authOptions.LdapConnectionString, username, password, AuthenticationTypes.Secure)) {
using (var ds = new DirectorySearcher(de, $"(&({AD_USERNAME}={username}))", new[] { AD_DISTINGUISHEDNAME })) {
try {
var result = ds.FindOne();
return result != null;
}
// Catch "username and password are invalid"
catch (DirectoryServicesCOMException ex) when (ex.ErrorCode == -2147023570) {
return false;
}
public ActiveDirectoryLoginProvider(AuthOptions authOptions) {
_authOptions = authOptions;
}

public bool Login(string username, string password) {
using (var de = new DirectoryEntry(_authOptions.LdapConnectionString, username, password, AuthenticationTypes.Secure)) {
using (var ds = new DirectorySearcher(de, $"(&({AD_USERNAME}={username}))", new[] { AD_DISTINGUISHEDNAME })) {
try {
var result = ds.FindOne();
return result != null;
}
// Catch "username and password are invalid"
catch (DirectoryServicesCOMException ex) when (ex.ErrorCode == -2147023570) {
return false;
}
}
}
}

public string GetUsernameFromEmailAddress(string email) {
using (var entry = new DirectoryEntry(_authOptions.LdapConnectionString)) {
using (var searcher = new DirectorySearcher(entry, $"(&({AD_EMAIL}={email}))", new[] { AD_USERNAME })) {
var result = searcher.FindOne();
return result?.Properties[AD_USERNAME][0].ToString();
}
public string GetUsernameFromEmailAddress(string email) {
using (var entry = new DirectoryEntry(_authOptions.LdapConnectionString)) {
using (var searcher = new DirectorySearcher(entry, $"(&({AD_EMAIL}={email}))", new[] { AD_USERNAME })) {
var result = searcher.FindOne();
return result?.Properties[AD_USERNAME][0].ToString();
}
}
}

public string GetEmailAddressFromUsername(string username) {
var result = FindUser(username);
return result?.Properties[AD_EMAIL][0].ToString();
}
public string GetEmailAddressFromUsername(string username) {
var result = FindUser(username);
return result?.Properties[AD_EMAIL][0].ToString();
}

public string GetUserFullName(string username) {
var result = FindUser(username);
if (result == null)
return null;
public string GetUserFullName(string username) {
var result = FindUser(username);
if (result == null)
return null;

return $"{result.Properties[AD_FIRSTNAME][0]} {result.Properties[AD_LASTNAME]}";
}
return $"{result.Properties[AD_FIRSTNAME][0]} {result.Properties[AD_LASTNAME]}";
}

private SearchResult FindUser(string username) {
using (var entry = new DirectoryEntry(_authOptions.LdapConnectionString)) {
using (var searcher = new DirectorySearcher(entry, $"(&({AD_USERNAME}={username}))", new[] { AD_FIRSTNAME, AD_LASTNAME, AD_EMAIL })) {
return searcher.FindOne();
}
private SearchResult FindUser(string username) {
using (var entry = new DirectoryEntry(_authOptions.LdapConnectionString)) {
using (var searcher = new DirectorySearcher(entry, $"(&({AD_USERNAME}={username}))", new[] { AD_FIRSTNAME, AD_LASTNAME, AD_EMAIL })) {
return searcher.FindOne();
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/Exceptionless.Core/Authentication/IDomainLoginProvider.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Exceptionless.Core.Authentication {
public interface IDomainLoginProvider {
bool Login(string username, string password);
namespace Exceptionless.Core.Authentication;

string GetEmailAddressFromUsername(string username);
public interface IDomainLoginProvider {
bool Login(string username, string password);

string GetUserFullName(string username);
string GetEmailAddressFromUsername(string username);

string GetUsernameFromEmailAddress(string email);
}
}
string GetUserFullName(string username);

string GetUsernameFromEmailAddress(string email);
}
22 changes: 11 additions & 11 deletions src/Exceptionless.Core/Authorization/AuthorizationRoles.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Exceptionless.Core.Authorization {
public static class AuthorizationRoles {
public const string ClientPolicy = nameof(ClientPolicy);
public const string Client = "client";
public const string UserPolicy = nameof(UserPolicy);
public const string User = "user";
public const string GlobalAdminPolicy = nameof(GlobalAdminPolicy);
public const string GlobalAdmin = "global";
public static readonly string[] AllScopes = { "client", "user", "global" };
}
}
namespace Exceptionless.Core.Authorization;

public static class AuthorizationRoles {
public const string ClientPolicy = nameof(ClientPolicy);
public const string Client = "client";
public const string UserPolicy = nameof(UserPolicy);
public const string User = "user";
public const string GlobalAdminPolicy = nameof(GlobalAdminPolicy);
public const string GlobalAdmin = "global";
public static readonly string[] AllScopes = { "client", "user", "global" };
}
Loading

0 comments on commit 7b641b0

Please sign in to comment.