Код: Выделить всё
WebAPI
Чтобы сохранить его коротким и сладким, у меня есть атрибут , выполняющий необходимую логику и пользовательскую аутентикуальную сервис , который указывает на мой хранилище данных . Архитектура < /code>: < /p>
api < /li>
Service < /li>
Business < /li>
Данные < /li>
< /ol>
Итак, я думаю, что первый вопрос, как я могу читать значения, кода /код /код, то есть
, так что я думаю, как я могу показать значения. (Извиняюсь в первый раз, используя претензии)
Код: Выделить всё
using (var authService = new AuthenticateService())
{
var client = await _authenticateService.AuthenticateAsync(
apiKey,
password);
if (client != null)
{
// Create a ClaimsIdentity with all the claims for this user.
Claim apiKeyClaim = new Claim("API Key", apiKey);
Claim clientNameClaim = new Claim(ClaimTypes.Name, client.ClientName);
Claim clientKeyClaim = new Claim("Client Key", client.ClientKey);
List claims = new List
{
apiKeyClaim,
clientNameClaim,
clientKeyClaim
};
// important to set the identity this way, otherwise IsAuthenticated will be false
// see: http://leastprivilege.com/2012/09/24/claimsidentity-isauthenticated-and-authenticationtype-in-net-4-5/
ClaimsIdentity identity = new ClaimsIdentity(claims, "Basic");
// AuthenticationTypes.Basic
var principal = new ClaimsPrincipal(identity);
return principal;
//var principal = new GenericPrincipal(new GenericIdentity("CustomIdentification"),
// new[] { "SystemUser" });
//return principal;
}
else
{
return null;
}
}
< /code>
Доступ к значениям претензий в моем контроллере API < /code>: < /p>
[IdentityBasicAuthentication]
[Authorize]
[RoutePrefix("api")]
public class OrderController : ApiController
{
private IOrderService _orderService;
public OrderController(IOrderService orderService)
{
_orderService = orderService;
}
// POST api/
[HttpPost]
[Route("order")]
public async Task Post([FromBody]Models.Model.Order order)
{
var modelResponse = new ModelResponse(order);
if (order == null)
return BadRequest("Unusable resource.");
if (!modelResponse.IsModelValid())
return this.PropertiesRequired(modelResponse.ModelErrors());
try
{
//Create abstracted Identity model to pass around layers
// Access Claim values here
//OR can I use Claims in other layers without creating an abstracted model to pass through.
await _orderService.AddAsync(order);
}
catch (System.Exception ex)
{
return InternalServerError();
}
finally
{
_orderService.Dispose();
}
return Ok("Order Successfully Processed.");
}
}
Подробнее здесь: https://stackoverflow.com/questions/425 ... sprincipal