Ejemplo n.º 1
0
        public static async Task <AppUser> GetUserAsync(string tenant, HubCallerContext context)
        {
            var token = await GetTokenAsync(tenant, context).ConfigureAwait(false);

            if (token != null)
            {
                await AppUsers.SetCurrentLoginAsync(tenant, token.LoginId).ConfigureAwait(false);

                var loginView = await AppUsers.GetCurrentAsync(tenant, token.LoginId).ConfigureAwait(false);

                return(new AppUser
                {
                    Tenant = tenant,
                    ClientToken = token.ClientToken,
                    LoginId = token.LoginId,
                    UserId = loginView.UserId,
                    OfficeId = loginView.OfficeId,
                    Email = loginView.Email,
                    IsAdministrator = loginView.IsAdministrator,
                    RoleId = loginView.RoleId,
                    Name = loginView.Name,
                    RoleName = loginView.RoleName,
                    OfficeName = loginView.OfficeName
                });
            }

            return(null);
        }
Ejemplo n.º 2
0
        protected override void Initialize(RequestContext context)
        {
            string tenant      = TenantConvention.GetTenant();
            string clientToken = context.HttpContext.Request.GetClientToken();
            var    provider    = new Provider();
            var    token       = provider.GetToken(clientToken);

            if (token != null)
            {
                bool isValid = AccessTokens.IsValidAsync(tenant, token.ClientToken, context.HttpContext.GetClientIpAddress(), context.HttpContext.GetUserAgent()).GetAwaiter().GetResult();

                if (isValid)
                {
                    AppUsers.SetCurrentLoginAsync(tenant, token.LoginId).GetAwaiter().GetResult();
                    var loginView = AppUsers.GetCurrentAsync(tenant, token.LoginId).GetAwaiter().GetResult();

                    this.AppUser = new AppUser
                    {
                        Tenant          = tenant,
                        ClientToken     = token.ClientToken,
                        LoginId         = loginView.LoginId,
                        UserId          = loginView.UserId,
                        Name            = loginView.Name,
                        OfficeId        = loginView.OfficeId,
                        OfficeName      = loginView.OfficeName,
                        Email           = loginView.Email,
                        RoleId          = loginView.RoleId,
                        RoleName        = loginView.RoleName,
                        IsAdministrator = loginView.IsAdministrator
                    };

                    var identity = new ClaimsIdentity(token.GetClaims(), DefaultAuthenticationTypes.ApplicationCookie,
                                                      ClaimTypes.NameIdentifier, ClaimTypes.Role);
                    identity.AddClaim(new Claim(ClaimTypes.NameIdentifier,
                                                token.LoginId.ToString(CultureInfo.InvariantCulture)));

                    if (loginView.RoleName != null)
                    {
                        identity.AddClaim(new Claim(ClaimTypes.Role, loginView.RoleName));
                    }

                    if (loginView.Email != null)
                    {
                        identity.AddClaim(new Claim(ClaimTypes.Email, loginView.Email));
                    }

                    context.HttpContext.User = new ClaimsPrincipal(identity);
                }
            }

            if (this.AppUser == null)
            {
                this.AppUser = new AppUser
                {
                    Tenant = tenant
                };
            }

            base.Initialize(context);
        }
Ejemplo n.º 3
0
        protected override void Initialize(HttpControllerContext context)
        {
            this.Tenant = TenantConvention.GetTenant();

            string clientToken = context.Request.GetBearerToken();
            var    provider    = new Provider();
            var    token       = provider.GetToken(clientToken);


            if (token != null)
            {
                AppUsers.SetCurrentLoginAsync(this.Tenant, token.LoginId).Wait();
                var loginView = AppUsers.GetCurrentAsync(this.Tenant, token.LoginId).Result;

                this.AppUser = new AppUser
                {
                    Tenant          = this.Tenant,
                    ClientToken     = token.ClientToken,
                    LoginId         = token.LoginId,
                    UserId          = loginView.UserId,
                    Name            = loginView.Name,
                    OfficeId        = loginView.OfficeId,
                    OfficeName      = loginView.OfficeName,
                    Email           = loginView.Email,
                    RoleId          = loginView.RoleId,
                    RoleName        = loginView.RoleName,
                    IsAdministrator = loginView.IsAdministrator
                };

                var identity = new ClaimsIdentity(token.GetClaims());

                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, token.LoginId.ToString(CultureInfo.InvariantCulture)));

                if (this.AppUser.RoleName != null)
                {
                    identity.AddClaim(new Claim(ClaimTypes.Role, this.AppUser.RoleName));
                }

                if (this.AppUser.Email != null)
                {
                    identity.AddClaim(new Claim(ClaimTypes.Email, this.AppUser.Email));
                }

                context.RequestContext.Principal = new ClaimsPrincipal(identity);
            }

            base.Initialize(context);
        }
Ejemplo n.º 4
0
        public override bool AuthorizeHubConnection(HubDescriptor descriptor, IRequest request)
        {
            string tenant = TenantConvention.GetTenant();

            string clientToken = request.GetClientToken();
            var    provider    = new Provider();
            var    token       = provider.GetToken(clientToken);

            if (token != null)
            {
                bool isValid = AccessTokens.IsValidAsync(tenant, token.ClientToken, request.GetClientIpAddress(), request.Headers["user-agent"]).GetAwaiter().GetResult();

                if (isValid)
                {
                    AppUsers.SetCurrentLoginAsync(tenant, token.LoginId).GetAwaiter().GetResult();
                    var loginView = AppUsers.GetCurrentAsync(tenant, token.LoginId).GetAwaiter().GetResult();

                    var identity = new ClaimsIdentity(token.GetClaims(), DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.NameIdentifier, ClaimTypes.Role);

                    identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, token.LoginId.ToString(CultureInfo.InvariantCulture)));

                    if (loginView.RoleName != null)
                    {
                        identity.AddClaim(new Claim(ClaimTypes.Role, loginView.RoleName));
                    }

                    if (loginView.Email != null)
                    {
                        identity.AddClaim(new Claim(ClaimTypes.Email, loginView.Email));
                    }

                    request.Environment["server.User"] = new ClaimsPrincipal(identity);
                    return(true);
                }
            }

            return(false);
        }