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

后端 未结 17 2390
爱一瞬间的悲伤
爱一瞬间的悲伤 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 05:05

    Here is how to workaround this with Java and Spring via a Filter:

    /**
     * To see why this is necessary, check out this page:
     * https://support.microsoft.com/en-gb/help/899927.
     */
    public class MicrosoftFilter extends OncePerRequestFilter {
      @Override
      protected void doFilterInternal(final HttpServletRequest request,
          final HttpServletResponse response,
          final FilterChain filterChain) throws ServletException, IOException {
        //Serve up a blank page to anything with a Microsoft Office user agent, forcing it to open the
        //URL in a browser instead of trying to pre-fetch it, getting redirected to SSO, and losing
        //the path of the original link.
        if (!request.getHeader("User-Agent").contains("ms-office")) {
          filterChain.doFilter(request, response);
        }
      }
    }
    
    /**
     * Security configuration.
     */
    @Configuration
    public class SecurityConfiguration {
      @Bean
      public FilterRegistrationBean microsoftFilterRegistrationBean() {
        FilterRegistrationBean<MicrosoftFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new MicrosoftFilter());
        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registrationBean;
      }
    }
    
    0 讨论(0)
  • 2020-11-28 05:06

    Server side this worked for me in IIS (using a rewrite rule)

    <rule name="WordBypass" enabled="true" stopProcessing="true">
        <match url=".*" />
        <conditions>
            <add input="{HTTP_USER_AGENT}" pattern="Word|Excel|PowerPoint|ms-office" />
        </conditions>
        <action type="CustomResponse" statusCode="200" statusReason="Refresh" statusDescription="Refresh" />
    </rule>
    
    0 讨论(0)
  • 2020-11-28 05:06

    I had to solve this issue for an ASP.NET site but I only wanted to use javascript/ jQuery:

    var isCoBrowse = ('<%= Session["user"].ToString().ToLower() %>' != '0');
    if (isCoBrowse && window.location.href.indexOf('ReturnUrl=') >= 0 && window.location.href.indexOf('dllCheq') == -1) {
        //redirect to the ReturnUrl & add dllCheq to the URI
        var toRedirect = decodeURIComponent(gup('ReturnUrl', window.location.href)) + '&dllCheq';
        window.location = toRedirect;
    }
    

    I got the gup function from: How to get the value from the URL parameter?

    0 讨论(0)
  • Fix for VB.NET:

    Dim userAgent As String = System.Web.HttpContext.Current.Request.UserAgent
    
    If userAgent.Contains("Word") Or userAgent.Contains("Excel") Or userAgent.Contains("PowerPoint") Or userAgent.Contains("ms-office") Then
           System.Web.HttpContext.Current.Response.Clear()
           System.Web.HttpContext.Current.Response.Write("<html><head><meta http-equiv='refresh' content='0'/></head><body></body></html>")
           System.Web.HttpContext.Current.Response.End()
    End If
    

    It basically forces the browser to refresh the page, so the request comes in with the user agent of the browser and all the correct cookies.

    0 讨论(0)
  • 2020-11-28 05:11

    I suspect this is a matter of how you are setting the cookie(s).

    Due to the nature of how the web was created, example.com is not seen as the same domain as www.example.com; hence: you can be logged in at www.example.com and not logged in at example.com.

    So in other words, check the URL in your word or excel file - is it the same domain as how you are logged in within your browser?

    There are two fixes/solutions to this cookie inconsistency: 1. redirect anyone who tries to load your site without the www. to the same page with the www. (or vice versa), or 2. when you are setting the cookie, make sure to specify the domain argument as ".example.com". The leading dot indicates the cookie should be valid on all subdomains of that domain as well.

    I suspect the reason the browser eventually recognizes it is because you probably eventually end up landing on a URL with the same domain structure as how you are logged in.

    Hope this helps.

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