Ejemplo n.º 1
0
 public AuthenticationApiClient(IApiEndpointConfig settings, ILogServiceAsync <ILogServiceSettings> logService)
     : base(settings, logService)
 {
     _appRepo              = new AppEndpointRepo(Settings, logService);
     _appUserRepo          = new AppUserEndpointRepo(Settings, logService);
     orgAppUserAuthIpRepo  = new AuthorizedIpEndpointRepo(Settings, logService);
     _orgAppUserRoleRepo   = new RolesEndpointRepo(Settings, logService);
     _userClaimsRepo       = new UserClaimsEndpointRepo(Settings, logService);
     _orgAppUserClaimsRepo = new OrgAppUserClaimsEndpointRepo(Settings, logService);
 }
Ejemplo n.º 2
0
        private async Task <AuthenticationTicket> CreateAuthenticationTicket(IOwinContext owinContext, ClaimsIdentity oAuthIdentity)
        {
            ILogServiceAsync <ILogServiceSettings> logService = GlobalConfiguration.Configuration.DependencyResolver.GetService <ILogServiceAsync <ILogServiceSettings> >();

            var sqlUserManager = owinContext.GetUserManager <ApplicationUserManager>();
            var sqlUser        = await sqlUserManager.FindByNameAsync(oAuthIdentity.Name);

            //http://www.c-sharpcorner.com/UploadFile/ff2f08/angularjs-enable-owin-refresh-tokens-using-asp-net-web-api/
            var newIdentity = new ClaimsIdentity(oAuthIdentity);

            //SqlServerClaimsGenerator claimsGenerator = new SqlServerClaimsGenerator(
            //    sqlUser,
            //    oAuthIdentity,
            //    owinContext.Get<RepoBase<SystemUser>>());

            //claimsGenerator.GenerateClaims();

            oAuthIdentity.AddClaim(new Claim("projectRequestRole", JsonConvert.SerializeObject(new SimpleRoleClaim
            {
                UserId                = 2154,                //_user.SystemUserID,
                DomainLogin           = "******",  //_user.DomainLogin.Trim(),
                UserName              = "******",      //_user.DomainLogin.Trim(),
                EmailAddress          = "*****@*****.**", //_user.EmailAddress.Trim(),
                DepartmentDescription = "some dp des",       //_user.DepartmentDescr,
                DepartmentId          = 33,                  //_user.DepartmentID,
                IsActive              = true,                //_user.boolIsActive,
                Role = "user",                               //_user.AppRole
            })));

            var currentUtc = new Microsoft.Owin.Infrastructure.SystemClock().UtcNow;

            AuthenticationProperties properties = CreateProperties(sqlUser);
            AuthenticationTicket     ticket     = new AuthenticationTicket(newIdentity, properties);

            ticket.Properties.IssuedUtc    = DateTime.UtcNow;
            ticket.Properties.ExpiresUtc   = currentUtc.Add(TimeSpan.FromDays(365));//TODO: configure token expiration time in web config
            ticket.Properties.AllowRefresh = true;

            var principal = new ClaimsPrincipal(new[] { oAuthIdentity });

            owinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            logService.LogMessage(new
            {
                type     = "claimsRefreshed",
                endpoint = owinContext.Request.Uri,
                userName = oAuthIdentity.Name,
                data     = new
                {
                    orgUsers = principal.GetClaim <SimpleRoleClaim>("projectRequestRole"),
                }
            });

            return(ticket);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            ILogServiceAsync <LogServiceOptions> logService = LogServiceAsync <LogServiceOptions> .Instance;

            List <int> x       = Enumerable.Repeat(1, 10000000).ToList();
            int        counter = 0;

            x.AsParallel().WithDegreeOfParallelism(100).ForAll(item =>
            {
                logService.LogMessage(new
                {
                    messag = String.Format("Loop count: {0}", counter++)
                });
            });
        }
Ejemplo n.º 4
0
 public HealthController(ILogServiceAsync <ILogServiceSettings> logService)
     : base(logService)
 {
 }
Ejemplo n.º 5
0
 public LogMessageEndpointRepo(IApiEndpointConfig settings, ILogServiceAsync <ILogServiceSettings> logService)
     : base(settings, logService)
 {
 }
Ejemplo n.º 6
0
 public EndpointRepo(IApiEndpointConfig settings, ILogServiceAsync <ILogServiceSettings> logService)
 {
     _settings   = settings;
     _logService = logService;
 }
Ejemplo n.º 7
0
 public UserClaimsEndpointRepo(IApiEndpointConfig settings, ILogServiceAsync <ILogServiceSettings> logService)
     : base(settings, logService)
 {
     _settings.BaseAddress = settings.BaseAddress;
 }
Ejemplo n.º 8
0
 public AuthorizedIpEndpointRepo(IApiEndpointConfig settings, ILogServiceAsync <ILogServiceSettings> logService)
     : base(settings, logService)
 {
     _settings.BaseAddress = settings.BaseAddress;
 }
Ejemplo n.º 9
0
 public BaseApiController(ILogServiceAsync <ILogServiceSettings> logService)
 {
     _logService = logService;
 }
Ejemplo n.º 10
0
 public HealthControllerBase(ILogServiceAsync <ILogServiceSettings> logService)
 {
     _logService = logService;
 }
Ejemplo n.º 11
0
 public LoggingApiClient(IApiEndpointConfig settings, ILogServiceAsync <ILogServiceSettings> logService)
     : base(settings, logService)
 {
     EndpointLogMessage = new LogMessageEndpointRepo(Settings, logService);
 }
Ejemplo n.º 12
0
 public BaseAuthClient(IApiEndpointConfig settings, ILogServiceAsync <ILogServiceSettings> logService)
 {
     _logService = logService;
     Settings    = settings;
 }
Ejemplo n.º 13
0
 public BaseAuthClient(IApiEndpointConfig settings)
 {
     Settings    = settings;
     _logService = LogServiceAsync <LogServiceOptions> .Instance;
 }
Ejemplo n.º 14
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            ILogServiceAsync <ILogServiceSettings> logService = GlobalConfiguration.Configuration.DependencyResolver.GetService <ILogServiceAsync <ILogServiceSettings> >();

            var sqlUserManager = context.OwinContext.GetUserManager <ApplicationUserManager>();
            var sqlUser        = await sqlUserManager.FindAsync(context.UserName, context.Password);

            if (sqlUser == null || !sqlUser.IsActive)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");

                logService.LogMessage(new
                {
                    type     = "claimsGenerated",
                    endpoint = context.Request.Uri,
                    userName = context.UserName,
                    data     = new
                    {
                        message = "invalid_grant The user name or password is incorrect",
                    }
                });

                return;
            }

            ClaimsIdentity oAuthIdentity = await sqlUser.GenerateUserIdentityAsync(sqlUserManager, OAuthDefaults.AuthenticationType);

            //ClaimsGenerator claimsGenerator = new ClaimsGenerator(
            //    sqlUser,
            //    oAuthIdentity,
            //    context.OwinContext.Get<RepoBase<SystemUser>>());

            //claimsGenerator.GenerateClaims();

            //claims generator is the same as this:

            oAuthIdentity.AddClaim(new Claim("projectRequestRole", JsonConvert.SerializeObject(new SimpleRoleClaim
            {
                UserId                = 2154,                //_user.SystemUserID,
                DomainLogin           = "******",  //_user.DomainLogin.Trim(),
                UserName              = "******",      //_user.DomainLogin.Trim(),
                EmailAddress          = "*****@*****.**", //_user.EmailAddress.Trim(),
                DepartmentDescription = "some dp des",       //_user.DepartmentDescr,
                DepartmentId          = 33,                  //_user.DepartmentID,
                IsActive              = true,                //_user.boolIsActive,
                Role = "user",                               //_user.AppRole
            })));

            ClaimsIdentity cookiesIdentity = await sqlUser.GenerateUserIdentityAsync(sqlUserManager, CookieAuthenticationDefaults.AuthenticationType);

            AuthenticationProperties properties = CreateProperties(sqlUser);
            AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);

            ticket.Properties.AllowRefresh = true;
            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);

            //TODO: Document this

            /*http://stackoverflow.com/questions/21971190/asp-net-web-api-2-owin-authentication-unsuported-grant-type/21979279#21979279
             *********************************
             * it wasn't enough adding config.EnableCors(new EnableCorsAttribute("*", "*", "*")); to WebApiConfig.cs
             * or the controllers. example:
             *     [EnableCors(origins: "*", headers: "*", methods: "*")]
             *     public class ValuesController : ApiController
             */

            var principal = new ClaimsPrincipal(new[] { oAuthIdentity });

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            logService.LogMessage(new
            {
                type     = "claimsGenerated",
                endpoint = context.Request.Uri,
                userName = context.UserName,
                data     = new
                {
                    orgUsers = principal.GetClaim <SimpleRoleClaim>("projectRequestRole"),
                }
            });
        }