Initial ...
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using xIdentityHelper;
|
||||
using xCommons.Authorization;
|
||||
using xCommons.Extensions;
|
||||
|
||||
namespace xApi.Extensions {
|
||||
public static class XStartupExtensions {
|
||||
/// <summary>
|
||||
/// Register Authorization Policies
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="policies"></param>
|
||||
public static void AddXAuthorization (
|
||||
this IServiceCollection services,
|
||||
IDictionary<string, AuthorizationPolicy> policies = null
|
||||
) {
|
||||
//
|
||||
services.AddAuthorization (options => {
|
||||
//
|
||||
if (!policies.IsNull ()) {
|
||||
//
|
||||
// Fill Policies ...
|
||||
using (var policiesEnumerator = policies.GetEnumerator ()) {
|
||||
while (policiesEnumerator.MoveNext ()) {
|
||||
var policyDescriptor = policiesEnumerator.Current;
|
||||
options.AddPolicy (policyDescriptor.Key, policyDescriptor.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
var xPolicies = XAuthorizationHelper.GetXAuthorizationPolicies ();
|
||||
if (!xPolicies.IsNull ()) {
|
||||
//
|
||||
// Fill Policies ...
|
||||
using (var policiesEnumerator = xPolicies.GetEnumerator ()) {
|
||||
while (policiesEnumerator.MoveNext ()) {
|
||||
var policyDescriptor = policiesEnumerator.Current;
|
||||
options.AddPolicy (policyDescriptor.Key, policyDescriptor.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
services.AddSingleton<IAuthorizationHandler, RequiredRolesHandler> ();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use Forward Headers Options for resolving behind a proxy issues ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static void UseXForwardOptions (this IApplicationBuilder source) {
|
||||
//
|
||||
var forwardOptions = new ForwardedHeadersOptions {
|
||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto,
|
||||
RequireHeaderSymmetry = false
|
||||
};
|
||||
|
||||
//
|
||||
forwardOptions.KnownNetworks.Clear ();
|
||||
forwardOptions.KnownProxies.Clear ();
|
||||
|
||||
//
|
||||
// ref: https://github.com/aspnet/Docs/issues/2384
|
||||
source.UseForwardedHeaders (forwardOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Constants;
|
||||
using xIdentityModels.Models;
|
||||
|
||||
namespace xApi.Extensions {
|
||||
public static class XUserClaimsInfoDtoExtensions {
|
||||
public static string GetUserSelectByParam (
|
||||
this XUserClaimsInfoDto source,
|
||||
XUserSelectBy userSelectBy = XUserSelectBy.Username
|
||||
) {
|
||||
//
|
||||
var result = "";
|
||||
|
||||
//
|
||||
switch (userSelectBy) {
|
||||
//
|
||||
case XUserSelectBy.ID:
|
||||
result = source.UserId;
|
||||
break;
|
||||
|
||||
//
|
||||
case XUserSelectBy.Username:
|
||||
result = source.UserName;
|
||||
break;
|
||||
|
||||
//
|
||||
case XUserSelectBy.Email:
|
||||
result = source.Email;
|
||||
break;
|
||||
|
||||
//
|
||||
case XUserSelectBy.MobileNumber:
|
||||
result = source.PhoneNumber;
|
||||
break;
|
||||
}
|
||||
|
||||
//
|
||||
if (result.IsNullOrEmpty ()) {
|
||||
//
|
||||
if (!source.UserId.IsNullOrEmpty ()) {
|
||||
result = source.UserId;
|
||||
} else if (!source.UserName.IsNullOrEmpty ()) {
|
||||
result = source.UserName;
|
||||
} else if (!source.Email.IsNullOrEmpty ()) {
|
||||
result = source.Email;
|
||||
} else if (!source.PhoneNumber.IsNullOrEmpty ()) {
|
||||
result = source.PhoneNumber;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user