首页 新闻 会员 周边 捐助

微信无法访问,其他浏览器正常

0
悬赏园豆:50 [已解决问题] 解决于 2018-08-06 17:45

微信浏览器每天会时而访问不了,一直在加载中,最后服务端的日志记录下只有如下内容

[16:28:24][Error][0|][An unhandled exception has occurred: The operation has timed out.|The operation has timed out.]

,但在其他浏览器是正常的

前端框架使用的是Framework7单页程序,@dudu 说是前端问题....

前端问题那就是其他浏览器应该可能出现无访问的情况吧

困扰了好久了!!!

微信登录代码如下:

复制代码
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Silver.Core;
using Silver.Core.Domain.Identity;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Extensions.Logging;

namespace Silver.Web.Framework.Attributes {
    public class WeChatAttribute : TypeFilterAttribute {
        public WeChatAttribute() : base(typeof(AsyncLoginImpl)) {
        }

        private class AsyncLoginImpl : IAsyncActionFilter {
            public AsyncLoginImpl(UserManager<User> userManager, SignInManager<User> signInManager, IConfiguration cfg, IHttpHelper http, ILogger<AsyncLoginImpl> logger) {
                _userManager = userManager;
                _signInManager = signInManager;
                _cfg = cfg;
                _http = http;
                _logger = logger;
            }

            private readonly UserManager<User> _userManager;
            private readonly SignInManager<User> _signInManager;
            private readonly IConfiguration _cfg;
            private readonly IHttpHelper _http;
            private readonly ILogger<AsyncLoginImpl> _logger;
            public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) {
                var httpContext = context.HttpContext;
                if (_signInManager.IsSignedIn(httpContext.User))
                    await next();
                else if (httpContext.Request.Headers["User-Agent"].ToString()
                             .IndexOf("MicroMessenger", StringComparison.OrdinalIgnoreCase) > 0) {
                    var query = httpContext.Request.Query;
                    var code = query.SingleOrDefault(c => c.Key == "code");
                    var appid = _cfg.GetValue<string>("Authentication:WeChat:AppID");
                    var appsecret = _cfg.GetValue<string>("Authentication:WeChat:AppSecret");if (string.IsNullOrEmpty(code.Value)) {
                        var redirect_uri = HttpUtility.UrlEncode(_cfg.GetValue<string>("Url"));
                        var url =
                            $"https://open.weixin.qq.com/connect/oauth2/authorize?appid={appid}&redirect_uri={redirect_uri}&response_type=code&scope=snsapi_base&state=login#wechat_redirect";
                        context.Result = new RedirectResult(url);
                    } else {var url = $"https://api.weixin.qq.com/sns/oauth2/access_token?appid={appid}&secret={appsecret}&code={code.Value}&grant_type=authorization_code";var data = _http.Get(url);if (data.IndexOf("access_token", StringComparison.Ordinal) > 0) {
                            var accessToken =
                                JsonConvert.DeserializeObject<AccessTokenModel>(data);
                            var user = _userManager.Users.SingleOrDefault(c => c.WeChatUserOpenId == accessToken.openid);
                            if (user == null) {
                                httpContext.Response.Cookies.Append("openid", accessToken.openid, new CookieOptions {
                                    Expires = DateTime.Now.AddMinutes(60)
                                });
                                await next();
                            } else {
                                await _signInManager.SignInAsync(user, true);
                                context.Result = new RedirectResult("/");
                            }
                        } else {
                            var errCode =
                                JsonConvert.DeserializeObject<ErrCodeModel>(data);
                            _logger.LogError($"请求openId发生错误,错误代码:{errCode.errcode},错误消息:{errCode.errmsg}");
                            await next();
                        }
                    }
                } else {
                    await next();
                }
            }
        }

        protected class AccessTokenModel {
            public string access_token { get; set; }
            public string expires_in { get; set; }
            public string refresh_token { get; set; }
            public string openid { get; set; }
            public string scope { get; set; }
        }

        protected class ErrCodeModel {
            public string errcode { get; set; }
            public string errmsg { get; set; }
        }

        protected class UserInfo {
            public string openid { get; set; }
            public string nickname { get; set; }
            public string sex { get; set; }
            public string province { get; set; }
            public string city { get; set; }
            public string country { get; set; }
            public string headimgurl { get; set; }
            public string unionid { get; set; }
        }
    }
}
复制代码

微信登录是否有问题?如果没问题请问如何排查啊?

问题补充:

即使已登录用户使用微信浏览器也会出现一直加载中,无法访问

但其他浏览器是正常的,cookies设置为7天

            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => {
                    options.Cookie.HttpOnly = true;
                    options.ExpireTimeSpan = TimeSpan.FromDays(7);
                    options.LoginPath = "/?route=loginregister";
                    options.SlidingExpiration = true;
                });

 

远扬的主页 远扬 | 初学一级 | 园豆:30
提问于:2018-07-21 18:45
< >
分享
最佳答案
0

确实是http请求出现超时,每次请求都要12秒左右

解决代码在这里 https://www.cnblogs.com/dudu/p/csharp-httpclient-attention.html#3223163

远扬 | 初学一级 |园豆:30 | 2018-08-06 17:44
其他回答(1)
0

服务端有对应的超时日志问题还是出在服务器,可以配置一下 Serilog 让其记录调用堆栈,配置方法见 .net core中如何让Serilog日志像控制台日志那样显示类名

收获园豆:50
dudu | 园豆:29618 (高人七级) | 2018-07-21 20:44

怀疑可能是这个地方在连接数据库服务器时发生了超时

var user = _userManager.Users.SingleOrDefault(c => c.WeChatUserOpenId == accessToken.openid);
支持(0) 反对(0) dudu | 园豆:29618 (高人七级) | 2018-07-21 20:54

@dudu:不是这里~因为即使已经登录用户使用微信内置浏览器访问也是无法访问,而其他浏览器均可访问这才奇怪

支持(0) 反对(0) 远扬 | 园豆:30 (初学一级) | 2018-07-21 22:51

@远扬: 您确认问题是出在 OnActionExecutionAsync 中吗?The operation has timed out 的异常是 在 OnActionExecutionAsync 中抛出的吗?

支持(0) 反对(0) dudu | 园豆:29618 (高人七级) | 2018-07-22 10:03

@dudu: 不确认,按照您的方法配置了下Serilog看看是否出在OnActionExecutionAsync再说~现在目前运行正常,没出现无法访问

支持(0) 反对(0) 远扬 | 园豆:30 (初学一级) | 2018-07-22 12:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册