//-----------------------------------------------------------------------
//
// Copyright (C) 2011 Microsoft Corporation
//
//-----------------------------------------------------------------------
namespace Microsoft.Samples.Management.OData.BasicPlugins
{
using System.Configuration;
using System.Globalization;
using System.Security.Principal;
using Microsoft.Management.Odata;
///
/// Basic CustomAuthorization implementation
/// Management OData service uses Microsoft.Management.Odata.CustomAuthorization interface to authorize a user.
/// This is a pass-through implementation which means it authorizes all users.
/// It gives same quota values for all users. The quota values can be overridden by following application settings:
/// MaxConcurrentRequests: Overrides maximum number of concurrent requests for a user
/// MaxRequestsPerTimeslot: Overrides maximum number of requests in a time slot
/// TimeslotSize: Override size of time slot (in seconds)
///
public class CustomAuthorization : Microsoft.Management.Odata.CustomAuthorization
{
///
/// Default value of max concurrent requests
///
private const int DefaultMaxConcurrentRequests = 10;
///
/// Default value of max request per time slot
///
private const int DefaultMaxRequestsPerTimeslot = 4;
///
/// Default time slot size
///
private const int DefaultTimeslotSize = 1;
///
/// Default managemnet system state key
///
private const string DefaultManagementSystemStateId = "E7D438A1-C0BA-49D6-952E-EF7C45CB737D";
///
/// Authorize a user.
///
/// Sender information
/// User quota value
/// User context in which to execute PowerShell cmdlet
public override WindowsIdentity AuthorizeUser(SenderInfo senderInfo, out UserQuota userQuota)
{
var maxConcurrentRequests = ConfigurationManager.AppSettings["MaxConcurrentRequests"];
var maxRequestsPerTimeslot = ConfigurationManager.AppSettings["MaxRequestsPerTimeslot"];
var timeslotSize = ConfigurationManager.AppSettings["TimeslotSize"];
userQuota = new UserQuota(
maxConcurrentRequests != null ? int.Parse(maxConcurrentRequests, CultureInfo.CurrentUICulture) : DefaultMaxConcurrentRequests,
maxRequestsPerTimeslot != null ? int.Parse(maxRequestsPerTimeslot, CultureInfo.CurrentUICulture) : DefaultMaxRequestsPerTimeslot,
timeslotSize != null ? int.Parse(timeslotSize, CultureInfo.CurrentUICulture) : DefaultTimeslotSize);
return WindowsIdentity.GetCurrent();
}
///
/// Gets membership id
///
/// Sender information
/// Always returns same membership id for all users which means all users are in same group
public override string GetMembershipId(SenderInfo senderInfo)
{
return DefaultManagementSystemStateId;
}
}
}