using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using xCommons.Attributes;
using xCommons.Configurations;
using xCommons.Providers;
using xIdentityHelper;
using xIdentityService.Controllers;
using xIdentityService.Interfaces;
namespace xApi.Controllers {
///
/// Test all Authentication Policies ...
///
[RequireXPowered (false)]
public class TestIdentity : XBaseIdentityController {
public TestIdentity (
ILogger logger,
XAppConfiguration appConfiguration,
IXIdentityProvider identityProvider,
XValidationProvider validationProvider
) : base (
logger,
appConfiguration,
identityProvider,
validationProvider
) { }
//
#region Test Actions ...
///
/// API Read Scope
///
/// string message
[HttpGet ("PassReadAccess")]
[Authorize (Policy = XPolicies.ReadAccess)]
public ActionResult PassReadAccess () {
return Ok ("Read Access Passed ...");
}
///
/// API Write Scope
///
/// string message
[HttpGet ("PassWriteAccess")]
[Authorize (Policy = XPolicies.WriteAccess)]
public ActionResult PassWriteAccess () {
return Ok ("Write Access Passed ...");
}
///
/// API Admin Scope
///
/// string message
[HttpGet ("PassAdminAccess")]
[Authorize (Policy = XPolicies.AdminAccess)]
public ActionResult PassAdminAccess () {
return Ok ("Admin Access Passed ...");
}
///
/// API Manage Scop
///
/// string message
[HttpGet ("PassManageAccess")]
[Authorize (Policy = XPolicies.ManageAccess)]
public ActionResult PassManageAccess () {
return Ok ("Manage Access Passed ...");
}
///
/// a simple Action which returns a List of Authenticated User Claims
///
/// string message which represent current user's claims
[HttpGet ("HiClaims")]
[Authorize (Policy = XPolicies.User)]
public ActionResult HiClaims () {
//
var result = new {
name = User.Identity.Name,
claims = User.Claims.Select (c => new {
c.Type, c.Value
})
};
//
return Ok (result);
}
///
/// a simple Hello User for Checking Authentication and Policy
///
/// string message which contains authenticated user name
[HttpGet ("HiUser")]
[Authorize (Policy = XPolicies.User)]
public ActionResult HiUser () {
//
var result = $"Hi User: {User.Identity.Name} ...";
//
return Ok (result);
}
///
/// a simple Hello User for Checking Authentication and Policy
///
/// string message which contains authenticated user name
[HttpGet ("HiEnabledUser")]
[Authorize (Policy = XPolicies.EnabledUser)]
public ActionResult HiEnabledUser () {
//
var result = $"Hi User: {User.Identity.Name} is Enabled ...";
//
return Ok (result);
}
///
/// a simple Hello User for Checking Authentication and Policy
///
/// string message which contains authenticated user name
[HttpGet ("HiAgent")]
[Authorize (Policy = XPolicies.Agent)]
public ActionResult HiAgent () {
//
var result = $"Hi Agent: {User.Identity.Name} ...";
//
return Ok (result);
}
///
/// a simple Hello User for Checking Authentication and Policy
///
/// string message which contains authenticated user name
[HttpGet ("HiEnabledAgent")]
[Authorize (Policy = XPolicies.EnabledAgent)]
public ActionResult HiEnabledAgent () {
//
var result = $"Hi Agent: {User.Identity.Name} is Enabled ...";
//
return Ok (result);
}
///
/// a simple Hello User for Checking Authentication and Policy
///
/// string message which contains authenticated user name
[HttpGet ("HiAdmin")]
[Authorize (Policy = XPolicies.Admin)]
public ActionResult HiAdmin () {
//
var result = $"Hi Admin: {User.Identity.Name} ...";
//
return Ok (result);
}
///
/// a simple Hello User for Checking Authentication and Policy
///
/// string message which contains authenticated user name
[HttpGet ("HiEnabledAdmin")]
[Authorize (Policy = XPolicies.EnabledAdmin)]
public ActionResult HiEnabledAdmin () {
//
var result = $"Hi Admin: {User.Identity.Name} is Enabled ...";
//
return Ok (result);
}
#endregion
}
}