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

后端 未结 17 2582
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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);
        }
    }
    

提交回复
热议问题