public LoginOutput Login([Required] string Code) { string apiUrl = string.Format(wxLoginApi, ConfigurationManager.AppSettings["wxAppid"], ConfigurationManager.AppSettings["wxAppsercret"], Code); JavaScriptSerializer js = new JavaScriptSerializer(); WechatLoginMsg msg = js.Deserialize <WechatLoginMsg>(HttpHelper.HttpGet(apiUrl)); if (!string.IsNullOrWhiteSpace(msg.Openid) && !string.IsNullOrWhiteSpace(msg.Session_key)) { LoginOutput output = _userAppService.WechatLogin(new WechatLoginInput { Openid = msg.Openid, Session_key = msg.Session_key }); return(output); } else { throw new UserFriendlyException(msg.Errcode, msg.Errmsg); } }
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); string wechatLoginKey = ConfigurationManager.AppSettings["wxLoginKey"]; if (string.IsNullOrWhiteSpace(wechatLoginKey)) { context.SetError("AppSettings", "Key:wxLoginKey is not found"); return(Task.FromResult <object>(null)); } var identity = new ClaimsIdentity("JWT"); if (context.Password == wechatLoginKey.Trim()) { string apiUrl = string.Format(wxLoginApi, ConfigurationManager.AppSettings["wxAppid"], ConfigurationManager.AppSettings["wxAppsercret"], context.UserName); JavaScriptSerializer js = new JavaScriptSerializer(); WechatLoginMsg msg = js.Deserialize <WechatLoginMsg>(HttpHelper.HttpGet(apiUrl)); //msg.Openid = "oqK0I0VG0jE5udoT1jIVBZOkQr3w"; //msg.Session_key = "87LCUedsESieDCbaABh/4g=="; if (!string.IsNullOrWhiteSpace(msg.Openid) && !string.IsNullOrWhiteSpace(msg.Session_key)) { using (var userAppService = IocManager.Instance.ResolveAsDisposable <IUserAppService>()) { LoginOutput output = userAppService.Object.WechatLogin(new WechatLoginInput { Openid = msg.Openid, Session_key = msg.Session_key }); identity.AddClaim(new Claim("UserId", output.UserId.ToString())); identity.AddClaim(new Claim("IsNewUser", output.IsNewUser.ToString())); if (!string.IsNullOrEmpty(output.NickName)) { identity.AddClaim(new Claim("nickname", output.NickName)); } if (!string.IsNullOrEmpty(output.UserName)) { identity.AddClaim(new Claim("username", output.UserName)); } if (!string.IsNullOrEmpty(output.UserType)) { identity.AddClaim(new Claim("usertype", output.UserType)); } } } else { context.SetError(msg.Errcode, msg.Errmsg); return(Task.FromResult <object>(null)); } } else { using (var userAppService = IocManager.Instance.ResolveAsDisposable <IUserAppService>()) { LoginOutput output = userAppService.Object.ManageLogin(new ManageLoginInput { PassWord = context.Password, UserName = context.UserName }); if (!output.UserId.HasValue) { context.SetError("invalid_grant", "The user name or password is incorrect"); return(Task.FromResult <object>(null)); } identity.AddClaim(new Claim("UserId", output.UserId.ToString())); identity.AddClaim(new Claim("IsNewUser", output.IsNewUser.ToString())); if (!string.IsNullOrEmpty(output.NickName)) { identity.AddClaim(new Claim("nickname", output.NickName)); } if (!string.IsNullOrEmpty(output.UserName)) { identity.AddClaim(new Claim("username", output.UserName)); } if (!string.IsNullOrEmpty(output.UserType)) { identity.AddClaim(new Claim("usertype", output.UserType)); } } } var props = new AuthenticationProperties(new Dictionary <string, string> { { "audience", context.ClientId ?? string.Empty } }); var ticket = new AuthenticationTicket(identity, props); context.Validated(ticket); return(Task.FromResult <object>(null)); }