首页新闻找找看学习计划

core发布后一直出现正在加载的情况

0
[待解决问题]

没有报任何错,就一直在这样加载,有遇到过的没

Oo柠柠柠酸柠檬o的主页 Oo柠柠柠酸柠檬o | 菜鸟二级 | 园豆:206
提问于:2018-07-16 11:33
< >
分享
所有回答(1)
0

不是 .net core 的问题,是代码的问题,代码中有没有使用 .Result

dudu | 园豆:41258 (高人七级) | 2018-07-16 11:36
private class AsyncLoginImpl : IAsyncActionFilter {
       public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) {
在这里使用context.Result是不是会出现上述情况?
    }
}
支持(0) 反对(0) 远扬 | 园豆:86 (初学一级) | 2018-07-16 15:18

@远扬: 使用 context.Result 没问题,这里有没有使用 await

支持(0) 反对(0) dudu | 园豆:41258 (高人七级) | 2018-07-16 16:31

@dudu: 没有使用await

那你所指的使用.Result是什么情况使用的?

支持(0) 反对(0) 远扬 | 园豆:86 (初学一级) | 2018-07-16 17:03

@远扬: 能否提供一下对应的 Controller Action 的代码

支持(0) 反对(0) dudu | 园豆:41258 (高人七级) | 2018-07-17 10:27

@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; }
        }
    }
}
支持(0) 反对(0) 远扬 | 园豆:86 (初学一级) | 2018-07-18 21:03

@远扬: 那应该是前端问题

支持(0) 反对(0) dudu | 园豆:41258 (高人七级) | 2018-07-21 15:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册