示例#1
0
 async public Task<AccountSessionData> AllocateAccessTokenAsync(AccountSessionData sessionData)
 {
     var db = redis.GetDatabase();
     sessionData.AccessToken = TokenUtil.GenerateToken(sessionData.Appkey, sessionData.AccountId);
     var key = TokenUtil.GenerateKeyOfToken(sessionData.Appkey, sessionData.AccountId, sessionData.AccessToken);
     var suc = await db.StringSetAsync(key, sessionData.ToJson());
     if (suc)
     {
         return sessionData;
     }
     throw new Exception("Allocate AccessToken Error");
 }
 public static IBahamutServiceProvider GetInstance(AccountSessionData UserSessionData)
 {
     return new TorontoServiceProviderUseMongoDb()
     {
         UserSessionData = UserSessionData
     };
 }
示例#3
0
 public async Task<bool> SetUserSessionDataAsync(AccountSessionData userSessionData)
 {
     var key = TokenUtil.GenerateKeyOfToken(userSessionData.Appkey, userSessionData.UserId, userSessionData.AppToken);
     return await redis.GetDatabase().StringSetAsync(key, userSessionData.ToJson(), AppTokenExipreTime);
 }
 public async Task<IActionResult> AjaxLogin(string username, string password, string appkey)
 {
     var authService = Startup.ServicesProvider.GetAuthenticationService();
     try
     {
         var result = authService.LoginValidate(username, password);
         if (result.Succeeded)
         {
             var appInstance = await Startup.AppServerInstanceMonitor.GetInstanceForClientWithAppkeyAsync(appkey);
             var tokenService = Startup.ServicesProvider.GetTokenService();
             var newSessionData = new AccountSessionData()
             {
                 AccountId = result.AccountID,
                 Appkey = appkey
             };
             var atokenResult = await tokenService.AllocateAccessTokenAsync(newSessionData);
             if (atokenResult == null)
             {
                 Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                 LogManager.GetCurrentClassLogger().Warn("AjaxLogin:Allocate Access Token Failed");
                 return Json(new { msg = "ALLOC_TOKEN_FAILED" });
             }
             var parameters = new
             {
                 loginSuccessed = "true",
                 accountID = result.AccountID,
                 accountName = result.AccountName,
                 bindMobile = result.ValidatedMobile,
                 bindEmail = result.ValidatedEmail,
                 accessToken = atokenResult.AccessToken,
                 appServerIP = appInstance.InstanceEndPointIP,
                 appServerPort = appInstance.InstanceEndPointPort,
                 appServiceUrl = appInstance.InstanceServiceUrl,
                 info = appInstance.InfoForClient
             };
             return Json(parameters);
         }
     }
     catch (NoAppInstanceException ex)
     {
         LogManager.GetLogger("Main").Error(ex, "AjaxLogin:No App Server Instance");
         return Json(new { msg = "NO_APP_INSTANCE" });
     }
     catch(LoginValidateException ex)
     {
         LogManager.GetCurrentClassLogger().Warn(ex, "AjaxLogin->{0}", ex.Message);
         Response.StatusCode = 403;
         return Json(new { msg = ex.Message });
     }
     catch (Exception ex)
     {
         LogManager.GetCurrentClassLogger().Error(ex, "AjaxLogin->{0}", ex.Message);
         Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
     }
     return Json(new { msg = "SERVER_ERROR" });
 }