public UserDto PostLogin([FromForm] LoginPo loginPo) { //1.查询用户信息 //2.判断用户信息是否存在 //3.将用户信息生成token进行存储 //4.将token信息存储到cookie或session //5.返回成功信息和token //6.对token进行认证 //1.获取identityServer接口文档 string userUrl = _dynamicMiddleUrl.GetMiddleUrl("https", "UserServices"); DiscoveryDocumentResponse discoveryDocument = _httpClient.GetDiscoveryDocumentAsync(userUrl).Result; if (discoveryDocument.IsError) { Console.WriteLine($"[DiscoveryDocumentResponse Error]: {discoveryDocument.Error}"); } //2.根据用户名和密码建立token TokenResponse tokenResponse = _httpClient.RequestPasswordTokenAsync(new PasswordTokenRequest() { Address = discoveryDocument.TokenEndpoint, ClientId = "client-password", ClientSecret = "secret", GrantType = "password", UserName = loginPo.UserName, Password = loginPo.Password }).Result; //3.返回AccessToken if (tokenResponse.IsError) { throw new BizException(tokenResponse.Error + "," + tokenResponse.Raw); } //4.获取用户信息 UserInfoResponse userInfoResponse = _httpClient.GetUserInfoAsync(new UserInfoRequest() { Address = discoveryDocument.UserInfoEndpoint, Token = tokenResponse.AccessToken }).Result; //5.返回UserDto信息 UserDto userDto = new UserDto(); userDto.UserId = userInfoResponse.Json.TryGetString("sub"); userDto.UserName = loginPo.UserName; userDto.AccessToken = tokenResponse.AccessToken; userDto.ExpiresIn = tokenResponse.ExpiresIn; // 1、加密方式有很多,证书加密,数据安全的 // 1、cookie存储 // 2、本地缓存 return(userDto); }
public async Task <LoginDto> Login(LoginPo loginPo) { if (string.IsNullOrWhiteSpace(loginPo?.UserName) || string.IsNullOrWhiteSpace(loginPo.Password)) { throw new BizException("1100", "用户名及密码不能为空"); } string identityUrl = _dynamicMiddleUrl.GetMiddleUrl("http", "IdentityServer"); //if (identityUrl.EndsWith(":80")) //{ // identityUrl = identityUrl.Replace(":80", ""); //} //DiscoveryDocumentResponse discoveryDocument = await _httpClient.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest() { Address = identityUrl, Policy = new DiscoveryPolicy() { RequireHttps = false } }); //if (discoveryDocument.IsError) //{ // Console.WriteLine($"[DiscoveryDocumentResponse Error]: {discoveryDocument.Error}"); //} // 根据用户名和密码建立token TokenResponse tokenResponse = await _httpClient.RequestPasswordTokenAsync(new PasswordTokenRequest() { Address = $"{identityUrl}/connect/token",// discoveryDocument.TokenEndpoint, ClientId = "PCMain", ClientSecret = "PCMainSecret", GrantType = "password", UserName = loginPo.UserName, Password = loginPo.Password }); if (tokenResponse.IsError) { throw new BizException(tokenResponse.Error, tokenResponse.ErrorDescription); } // 获取用户信息 //UserInfoResponse userInfoResponse = _httpClient.GetUserInfoAsync(new UserInfoRequest() //{ // Address = discoveryDocument.UserInfoEndpoint, // Token = tokenResponse.AccessToken //}).Result; // 返回UserDto信息 LoginDto userDto = new LoginDto(); //userDto.UserId = Convert.ToInt64(userInfoResponse.Json.TryGetString("sub")); userDto.UserName = loginPo.UserName; userDto.AccessToken = tokenResponse.AccessToken; userDto.ExpiresIn = tokenResponse.ExpiresIn; return(userDto); }
public UserDto PostLogin([FromForm] LoginPo loginPo) { // 1、查询用户信息 // 2、判断用户信息是否存在 // 3、将用户信息生成token进行存储 // 4、将token信息存储到cookie或者session中 // 5、返回成功信息和token // 6、对于token进行认证(也就是身份认证) // 1、获取IdentityServer接口文档 HttpClient client = new HttpClient(); DiscoveryDocumentResponse discoveryDocument = client.GetDiscoveryDocumentAsync("https://localhost:5005").Result; if (discoveryDocument.IsError) { Console.WriteLine($"[DiscoveryDocumentResponse Error]: {discoveryDocument.Error}"); } // 2、根据用户名和密码建立token TokenResponse tokenResponse = client.RequestPasswordTokenAsync( new PasswordTokenRequest { Address = discoveryDocument.TokenEndpoint, ClientId = "client-password", ClientSecret = "secret", GrantType = "password", UserName = loginPo.UserName, Password = loginPo.Password }).Result; // 3、返回AccessToken if (tokenResponse.IsError) { throw new BizException(tokenResponse.Error + "," + tokenResponse.Raw); } //4.返回UserDto信息 UserDto userDto = new UserDto(); userDto.UserName = loginPo.UserName; userDto.AccessToken = tokenResponse.AccessToken; userDto.Exprise = tokenResponse.ExpiresIn; return(userDto); }
public UserDto PostLogin(LoginPo loginPo) { var wli = new WechatLoginInfo(); wli.code = loginPo.code; wli.encryptedData = loginPo.encryptedData; wli.iv = loginPo.iv; wli.rawData = loginPo.rawData; wli.signature = loginPo.signature; WechatUserInfo wechatResult = new WeChatAppDecrypt().Decrypt(wli); if (wechatResult == null || string.IsNullOrWhiteSpace(wechatResult.openId)) { throw new BizException("授权失败"); } wechatResult.nickName = HttpUtility.UrlEncode(wechatResult.nickName); var userInfo = userClient.GetUserByOpenId(wechatResult.openId); var userid = 0; if (null == userInfo || userInfo.Id <= 0) { var model = new User() { CreateTime = DateTime.Now, Phone = wechatResult.phoneNumber, wx_HeadImgUrl = wechatResult.avatarUrl, wx_NickName = wechatResult.nickName, wx_OpenID = wechatResult.openId, wx_UnionID = wechatResult.unionId, HasAuthenticated = false, RoleName = "1", Status = 1 }; var obj = userClient.PostUser(model); if (obj == null || obj.Id <= 0) { throw new BizException("用户新增失败"); } else { userid = obj.Id; } } else { userid = userInfo.Id; } // 1、获取IdentityServer接口文档 string userUrl = dynamicMiddleUrl.GetMiddleUrl("https", "UserServices"); DiscoveryDocumentResponse discoveryDocument = httpClient.GetDiscoveryDocumentAsync(userUrl).Result; if (discoveryDocument.IsError) { Console.WriteLine($"[DiscoveryDocumentResponse Error]: {discoveryDocument.Error}"); } // 2、根据用户名和密码建立token TokenResponse tokenResponse = httpClient.RequestPasswordTokenAsync(new PasswordTokenRequest() { Address = discoveryDocument.TokenEndpoint, ClientId = "client-password", ClientSecret = "secret", GrantType = "password", UserName = userid.ToString(), Password = userInfo.Phone }).Result; // 3、返回AccessToken if (tokenResponse.IsError) { throw new BizException(tokenResponse.Error + "," + tokenResponse.Raw); } // 4、获取用户信息 UserInfoResponse userInfoResponse = httpClient.GetUserInfoAsync(new UserInfoRequest() { Address = discoveryDocument.UserInfoEndpoint, Token = tokenResponse.AccessToken }).Result; // 5、返回UserDto信息 UserDto userDto = new UserDto(); userDto.UserId = userInfoResponse.Json.TryGetString("sub"); userDto.UserName = loginPo.UserName; userDto.AccessToken = tokenResponse.AccessToken; userDto.ExpiresIn = tokenResponse.ExpiresIn; return(userDto); }