예제 #1
0
        public async Task <ActionResult> OnGet()
        {
            var code = Request.Query["code"];
            var UserAccessTokenResult = await wxService.GetMPAccessToken(code);

            HttpContext.Session.SetString("AccessToken", JsonConvert.SerializeObject(UserAccessTokenResult));

            var userInfo = await wxService.GetUserInfo(UserAccessTokenResult.AccessToken, UserAccessTokenResult.OpenId);

            var hasUser = await userManager.FindByLoginAsync("wxMP", UserAccessTokenResult.OpenId);

            if (hasUser == null)
            {
                var user = new MemberAccount
                {
                    UserName           = "******" + Guid.NewGuid().ToString(),
                    NormalizedUserName = userInfo.NickName
                };

                await userManager.CreateAsync(user, "WechatPassword123#");

                await userManager.AddLoginAsync(user, new UserLoginInfo("wxMP", UserAccessTokenResult.OpenId, "Wechat"));

                await userManager.AddClaimAsync(user, new System.Security.Claims.Claim("wxopenid", UserAccessTokenResult.OpenId));

                await signInManager.SignInAsync(user, true);
            }
            else
            {
                await signInManager.SignInAsync(hasUser, true);
            }


            return(Redirect("/"));
        }