Exemple #1
0
        public static bool LoginAzureADAuthentication(ActionContext ac, string jwt)
        {
            using (AuthLogic.Disable())
            {
                try
                {
                    var ada = (ActiveDirectoryAuthorizer)AuthLogic.Authorizer !;

                    if (!ada.GetConfig().LoginWithAzureAD)
                    {
                        return(false);
                    }

                    var principal = ValidateToken(jwt, out var jwtSecurityToken);
                    var ctx       = new AzureClaimsAutoCreateUserContext(principal);

                    UserEntity?user =
                        Database.Query <UserEntity>().SingleOrDefault(a => a.Mixin <UserOIDMixin>().OID == ctx.OID);

                    if (user == null)
                    {
                        user = Database.Query <UserEntity>().SingleOrDefault(a => a.UserName == ctx.UserName) ??
                               (ctx.UserName.Contains("@") && ada.GetConfig().AllowSimpleUserNames ? Database.Query <UserEntity>().SingleOrDefault(a => a.UserName == ctx.UserName.Before("@")) : null);

                        if (user != null && user.Mixin <UserOIDMixin>().OID == null)
                        {
                            user.Mixin <UserOIDMixin>().OID = ctx.OID;
                            using (AuthLogic.Disable())
                                using (OperationLogic.AllowSave <UserEntity>())
                                {
                                    user.Save();
                                }
                        }
                    }

                    if (user == null)
                    {
                        user = ada.OnAutoCreateUser(ctx);

                        if (user == null)
                        {
                            return(false);
                        }
                    }

                    AuthServer.OnUserPreLogin(ac, user);
                    AuthServer.AddUserSession(ac, user);
                    return(true);
                }
                catch
                {
                    return(false);
                }
            }
        }
        public static bool LoginAzureADAuthentication(ActionContext ac, string jwt, bool throwErrors)
        {
            using (AuthLogic.Disable())
            {
                try
                {
                    var ada = (ActiveDirectoryAuthorizer)AuthLogic.Authorizer !;

                    if (!ada.GetConfig().LoginWithAzureAD)
                    {
                        return(false);
                    }

                    var principal = ValidateToken(jwt, out var jwtSecurityToken);
                    var ctx       = new AzureClaimsAutoCreateUserContext(principal);

                    UserEntity?user = Database.Query <UserEntity>().SingleOrDefault(a => a.Mixin <UserADMixin>().OID == ctx.OID);

                    if (user == null)
                    {
                        user = Database.Query <UserEntity>().SingleOrDefault(a => a.UserName == ctx.UserName) ??
                               (ctx.UserName.Contains("@") && ada.GetConfig().AllowMatchUsersBySimpleUserName ? Database.Query <UserEntity>().SingleOrDefault(a => a.Email == ctx.UserName || a.UserName == ctx.UserName.Before("@")) : null);
                    }

                    if (user == null)
                    {
                        user = ada.OnAutoCreateUser(ctx);

                        if (user == null)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        ada.UpdateUser(user, ctx);
                    }

                    AuthServer.OnUserPreLogin(ac, user);
                    AuthServer.AddUserSession(ac, user);
                    return(true);
                }
                catch (Exception ex)
                {
                    ex.LogException();
                    if (throwErrors)
                    {
                        throw;
                    }

                    return(false);
                }
            }
        }