public virtual AccessToken GetAccessToken( AuthConnect connect, String code, String httpMethod ) { StringBuilder sb = new StringBuilder(); sb.Append( connect.AccessTokenUrl ); sb.AppendFormat( "?client_id={0}", connect.ConsumerKey ); sb.AppendFormat( "&client_secret={0}", connect.ConsumerSecret ); sb.AppendFormat( "&code={0}", code ); sb.AppendFormat( "&redirect_uri={0}", connect.CallbackUrl ); sb.Append( "&grant_type=authorization_code" ); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create( sb.ToString() ); request.Method = httpMethod; try { logger.Info( "begin request access token: " + sb.ToString() ); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader( responseStream )) { return getAccessTokenByResponse( reader.ReadToEnd() ); } } } } catch (WebException wex) { HttpClientExceptionHelper.throwWebException( sb.ToString(), wex ); } catch (Exception ex) { HttpClientExceptionHelper.throwOtherException( sb.ToString(), ex ); } return null; }
public virtual AccessToken GetAccessToken(AuthConnect connect, String code, String httpMethod) { StringBuilder sb = new StringBuilder(); sb.Append(connect.AccessTokenUrl); sb.AppendFormat("?client_id={0}", connect.ConsumerKey); sb.AppendFormat("&client_secret={0}", connect.ConsumerSecret); sb.AppendFormat("&code={0}", code); sb.AppendFormat("&redirect_uri={0}", connect.CallbackUrl); sb.Append("&grant_type=authorization_code"); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sb.ToString()); request.Method = httpMethod; try { logger.Info("begin request access token: " + sb.ToString()); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream)) { return(getAccessTokenByResponse(reader.ReadToEnd())); } } } } catch (WebException wex) { HttpClientExceptionHelper.throwWebException(sb.ToString(), wex); } catch (Exception ex) { HttpClientExceptionHelper.throwOtherException(sb.ToString(), ex); } return(null); }
private static Dictionary <String, AuthConnect> loadConnects() { Dictionary <String, AuthConnect> map = new Dictionary <String, AuthConnect>(); foreach (KeyValuePair <String, Type> kv in ObjectContext.Instance.TypeList) { Type t = kv.Value; if (t.IsSubclassOf(typeof(AuthConnect))) { AuthConnectConfig cfgConnect = AuthConnectConfig.GetByType(t.FullName); if (cfgConnect == null) { logger.Warn(String.Format("config of '{0}' is empty", t.FullName)); continue; } AuthConnect obj = initConnect(t, cfgConnect); map.Add(t.FullName, obj); } } return(map); }
public virtual Result Sync(int userId, String connectType, int isSync) { Result result = new Result(); AuthConnect connect = AuthConnectFactory.GetConnect(connectType); if (connect == null) { result.Add("此连接类型不存在:" + connectType); return(result); } UserConnect x = GetConnectInfo(userId, connect.GetType().FullName); if (x == null) { result.Add("对不起,您没有绑定过"); return(result); } x.NoSync = (isSync == 0 ? 1 : 0); x.update(); return(result); }
public virtual Result UnBind(int userId, String connectType) { Result result = new Result(); User user = User.findById(userId); if (user == null) { result.Add("用户不存在,无法取消绑定"); return(result); } AuthConnect connect = AuthConnectFactory.GetConnect(connectType); if (connect == null) { result.Add("此连接类型不存在:" + connectType); return(result); } UserConnect x = GetConnectInfo(userId, connect.GetType().FullName); if (x == null) { result.Add("对不起,您没有绑定过"); return(result); } // 如果这是最后一个绑定,并且用户没有补充过用户名和密码,那么禁止取消绑定。 if (strUtil.IsNullOrEmpty(user.Pwd) && isLastBind(user)) { result.Add("这是您的最后一个绑定,取消之后将无法登录。请在“修改密码”页面补充密码之后,再取消绑定。"); return(result); } int rowAffected = x.delete(); result.Info = rowAffected; if (rowAffected == 1) { user.IsBind = 0; user.update(); } return(result); }
private static AuthConnect initConnect(Type t, AuthConnectConfig x) { AuthConnect obj = ObjectContext.Create(t) as AuthConnect; if (strUtil.IsNullOrEmpty(x.ConsumerKey)) { throw new Exception(String.Format("请设置 Consumer Key: {0}", x.Name)); } if (strUtil.IsNullOrEmpty(x.ConsumerSecret)) { throw new Exception(String.Format("请设置 Consumer Secret: {0}", x.Name)); } obj.ConsumerKey = x.ConsumerKey; obj.ConsumerSecret = x.ConsumerSecret; return(obj); }
private void processLogin( AuthConnect connect, String code ) { if (ctx.viewer.IsLogin) { echoError( "对不起,您已经登录" ); return; } AccessToken accessToken = OAuthClient.New().GetAccessToken( connect, code, connect.HttpMethod_AccessToken ); logger.Info( "accessToken=" + accessToken.Token ); logger.Info( "uid=" + accessToken.Uid ); logger.Info( "refresh_token=" + accessToken.RefreshToken ); logger.Info( "expires_in=" + accessToken.ExpiresIn ); logger.Info( "scope=" + accessToken.Scope ); String uid = connect.GetUid( accessToken ); // 1) 检查网站中是否有此用户 UserConnect x = connectService.GetConnectInfo( uid, connect.GetType().FullName ); // 第一次登录 if (x == null) { try { loadUserProfile( connect, accessToken ); } catch (HttpClientException ex) { if (ex.Message.IndexOf( "applications over the unaudited use restrictions" ) > 0) { echo( getTestRestrictionsMsg( "登录" ) ); } else { throw ex; } } } // 其他:获取用户信息,然后登录 else { checkAccessToken( x, accessToken ); LoginTime expiration = LoginTime.OneWeek; loginService.Login( x.User, x.Id, expiration, ctx.Ip, ctx ); echoRedirect( "登录成功", "/" ); } }
private void processBind( AuthConnect connect, String code ) { if (ctx.viewer.IsLogin == false) { echoError( "对不起,请先登录" ); return; } // 检查是否绑定过 if (connectService.HasBind( ctx.viewer.Id, connect.GetType().FullName )) { echoError( "对不起,已经绑定" ); return; } // 获取用户 uid AccessToken x = OAuthClient.New().GetAccessToken( connect, code, connect.HttpMethod_AccessToken ); x.Uid = connect.GetUid( x ); // 获取用户名称 OAuthUserProfile userProfile = null; try { userProfile = connect.GetUserProfile( x ); } catch (HttpClientException ex) { if (ex.Message.IndexOf( "applications over the unaudited use restrictions" ) > 0) { echo( getTestRestrictionsMsg( "绑定" ) ); return; } else { throw ex; } } x.Name = userProfile.Name; Result result = connectService.Create( ctx.viewer.obj as User, connect.GetType().FullName, x ); // 日志 logger.Info( "accessToken=" + x.Token ); logger.Info( "uid=" + x.Uid ); logger.Info( "refresh_token=" + x.RefreshToken ); logger.Info( "expires_in=" + x.ExpiresIn ); logger.Info( "scope=" + x.Scope ); if (result.HasErrors) { echo( result.ErrorsHtml ); } else { echoRedirect( lang( "opok" ), "/" ); } }
private void loadUserProfile( AuthConnect connect, AccessToken accessToken ) { OAuthUserProfile user = connect.GetUserProfile( accessToken ); ctx.SetItem( "__currentOAuthUser", user ); ctx.SetItem( "__currentAccessToken", accessToken ); content( loadHtml( confirmUserInfo ) ); }
public virtual AccessToken GetAccessToken( AuthConnect connect, String code ) { return GetAccessToken( connect, code, HttpMethod.Post ); }
public virtual AccessToken GetAccessToken(AuthConnect connect, String code) { return(GetAccessToken(connect, code, HttpMethod.Post)); }
public virtual AccessToken GetAccessToken( AuthConnect connect, String code, String httpMethod ) { return oauthHelper.GetAccessToken( connect, code, httpMethod ); }
public virtual AccessToken GetAccessToken( AuthConnect connect, String code ) { return oauthHelper.GetAccessToken( connect, code ); }
public virtual AccessToken GetAccessToken(AuthConnect connect, String code, String httpMethod) { return(oauthHelper.GetAccessToken(connect, code, httpMethod)); }
public virtual AccessToken GetAccessToken(AuthConnect connect, String code) { return(oauthHelper.GetAccessToken(connect, code)); }