Why are cookies unrecognized when a link is clicked from an external source (i.e. Excel, Word, etc…)

后端 未结 17 2389
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 04:39

I noticed that when a link is clicked externally from the web browser, such as from Excel or Word, that my session cookie is initially unrecognized, even if the link opens u

相关标签:
17条回答
  • 2020-11-28 04:47

    PHP solution:

    This prevents the MS product recognising the redirect. MS therefore launches a browser from the required link.

    if (isset($_SERVER['HTTP_USER_AGENT']))
    {
        $http_user_agent = $_SERVER['HTTP_USER_AGENT']; 
        if (preg_match('/Word|Excel|PowerPoint|ms-office/i', $http_user_agent)) 
        {
            // Prevent MS office products detecting the upcoming re-direct .. forces them to launch the browser to this link
            die();
        }
    }
    

    .. redirect after this code

    0 讨论(0)
  • 2020-11-28 04:49

    Here is an example of the fix using a dotnet core middleware:

    public class MicrosoftOfficeLinksHandlingMiddleware
    {
        private static readonly Regex MsUserAgentsRegex = new Regex(@"[^\w](Word|Excel|PowerPoint|ms-office)([^\w]|\z)");
        private readonly RequestDelegate _next;
    
        public MicrosoftOfficeLinksHandlingMiddleware(RequestDelegate next)
        {
            _next = next;
        }
    
        public async Task Invoke(HttpContext context)
        {
            string userAgent = context.Request.Headers["User-Agent"].FirstOrDefault();
    
            if (userAgent != null && MsUserAgentsRegex.IsMatch(userAgent))
            {
                // just return an empty response to the office agent
                return;
            }
    
            await _next(context);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 04:49

    Here is my solution for this in WordPress. Add this to functions.php in your theme or another plugin file.

    This may be helpful if your system, like WP, sends logged out users to a login page with a redirect to the page they were trying to access. Word was sending users to this page, but then WP wasn't properly handling the case where a user was already logged in. This code checks if there is a current user and a redirect_to param passed. If so, it redirects to the redirect_to location.

    function my_logged_in_redirect_to()
    {
    global $current_user;
    
    if($current_user->ID && $_REQUEST['redirect_to'])
    {           
        wp_redirect($_REQUEST['redirect_to']);
        exit;
    }
    }
    add_action('wp', 'my_logged_in_redirect_to');
    
    0 讨论(0)
  • 2020-11-28 04:52

    Here is a solution for C# ASP.NET based on spilliton's answer above. In Global.asax.cs, add the following:

        private static string MSUserAgentsRegex = @"[^\w](Word|Excel|PowerPoint|ms-office)([^\w]|\z)";
        protected void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(Request.UserAgent, MSUserAgentsRegex))
            {
                Response.Write("<html><head><meta http-equiv='refresh' content='0'/></head><body></body></html>");
                Response.End();
            }
        }
    
    0 讨论(0)
  • 2020-11-28 04:53

    Here's a VBA fix, for Excel. The same concept can be applied for Microsoft Word. Basically, rather than firing off the link from within Excel, the code executes the link from within a shell. Here's the code:

    Private Sub Worksheet_FollowHyperlink(ByVal objLink As Hyperlink)
        Application.EnableEvents = False
        Dim strAddress As String
        strAddress = "explorer " & objLink.TextToDisplay
        Dim dblReturn As Double
        dblReturn = Shell(strAddress)
        Application.EnableEvents = True
    End Sub
    
    1. For the Excel sheet that contains the links, right-click the sheet tab and click View Code. The VBA editor appears.
    2. Paste the code into the window, and close the editor.
    3. Modify each link in the page so it simply points back to the cell that it is in. To do so:
    4. Right-click the link, and click Edit Hyperlink. An Edit Hyperlink window appears.
    5. Click Place In This Document.
    6. Click the sheet name.
    7. For Type the cell reference, enter a cell reference (e.g. A4).
    8. Click OK.

    A couple of notes:

    • You will need to save the spreadsheet as a macro-enabled spreadsheet (.xlsm). When users open the spreadsheet, they will be asked to enable macros. If they answer No, the links will not work.
    • These instructions are based on Excel 2010. Presumably later versions are similar.
    0 讨论(0)
  • 2020-11-28 04:56

    I can't believe they call this a feature. However, here's a featurefix for Apache:

    RewriteEngine On
    
    # Send a 200 to MS Office so it just hands over control to the browser
    # It does not use existing session cookies and would be redirected to the login page otherwise
    # https://www.wimpyprogrammer.com/microsoft-office-link-pre-fetching-and-single-sign-on/
    
    RewriteCond %{HTTP_USER_AGENT} ;\sms-office(\)|;)
    RewriteRule .* - [R=200,L]
    

    Might not be best performance wise, as the whole page gets sent instead of an empty response, but I did not want to add another Apache modules just for fixing such an idio^H^H^H^H feature.

    0 讨论(0)
提交回复
热议问题