Anshi A

Anshi A

  • 1.6k
  • 26
  • 1k

Too many redirects error in GoDaddy server

Jul 1 2020 7:47 AM
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// SRAJ 26 Aug 2019 : Access the token and check time expiry and rest the token key
Credential obj = new Credential();
string baseAddress = ConfigurationManager.AppSettings["APIServiceUrl"].ToString();
try
{
string gettokentocheck = null;
serviceObj = new ServiceRepository();
if (filterContext.HttpContext.Session != null)
{
var UserName = filterContext.HttpContext.Session["UserName"] != null ? filterContext.HttpContext.Session["UserName"].ToString() : "" ;
if (UserName != null && UserName != "")
{
UserName1 = UserName.ToString();
HttpContext.Current.Session["UserName"] = UserName1.ToString();
}
var Password = filterContext.HttpContext.Session["Password"] != null ? filterContext.HttpContext.Session["Password"].ToString() : "";
if (Password != null && Password!="")
{
Password1 = Password.ToString();
HttpContext.Current.Session["Password"] = Password1.ToString();
}
var SSOEnabled = filterContext.HttpContext.Session["SSOEnabled"] != null ? filterContext.HttpContext.Session["SSOEnabled"].ToString() : "";
if (SSOEnabled != null && SSOEnabled != "")
{
strSSOEnabled = SSOEnabled.ToString();
HttpContext.Current.Session["SSOEnabled"] = strSSOEnabled.ToString();
}
}
//string uname= filterContext.Controller.ControllerContext.
var form = new Dictionary<string, string>();
if (strSSOEnabled == "True")
{
form = new Dictionary<string, string>
{
{"grant_type", "password"},
{"username", UserName1},
{"password",""},
};
}
else
{
form = new Dictionary<string, string>
{
{"grant_type", "password"},
{"username", UserName1},
{"password",Password1},
};
}
DateTime expiryOn = ConfigurationManager.AppSettings["APITimeExpiry"] == "" ? DateTime.UtcNow : Convert.ToDateTime(ConfigurationManager.AppSettings["APITimeExpiry"]);
if (expiryOn <= DateTime.UtcNow)
{
Token token = new Token(); // Generate token to access web api method
using (var client = new HttpClient())
{
// client.DefaultRequestHeaders
//.Accept
//.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));//ACCEPT header
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
var tokenResponse = client.PostAsync(baseAddress + "/token", new FormUrlEncodedContent(form)).Result;
token = tokenResponse.Content.ReadAsAsync<Token>(new[] { new JsonMediaTypeFormatter() }).Result;
}
if (token.AccessToken != null)
{
gettokentocheck = token.AccessToken;
ConfigurationManager.AppSettings["APIToken"] = token.AccessToken; // Store token key in appsetting [web config file]
ConfigurationManager.AppSettings["APITimeExpiry"] = DateTime.UtcNow.AddSeconds(token.ExpiresIn).ToString(); // Store token expiry time in appsetting [web config file]
}
else
{
ConfigurationManager.AppSettings["APIToken"] = token.AccessToken;
}
}
//Api client response
if (HttpContext.Current.Session["Role"] == null)
{
ReturnMethod(filterContext);
return;
}
else
{
if (HttpContext.Current.Session["Role"].ToString() != "Global Administration")
{
if (HttpContext.Current.Session["OrganizationID"] == null || HttpContext.Current.Session["UserID"] == null || HttpContext.Current.Session["UserName"] == null)
{
ReturnMethod(filterContext);
return;
}
}
else
{
if (HttpContext.Current.Session["GlobalUserID"] == null || HttpContext.Current.Session["GlobalUserName"] == null)
{
ReturnMethod(filterContext);
return;
}
}
}
HttpResponseMessage response = serviceObj.GetResponse("api/SessionConfig/GetSessionConfig");
response.EnsureSuccessStatusCode();
CTSIWEB.CommonModelClass.SessionConfig sRes = response.Content.ReadAsAsync<CTSIWEB.CommonModelClass.SessionConfig>().Result;
}
catch (Exception e)
{
ReturnMethod(filterContext);
}
}
public void ReturnMethod(ActionExecutingContext filterContext)
{
FormsAuthentication.SignOut();
var LastUrl = "";
LastUrl = filterContext.HttpContext.Request.RawUrl;
filterContext.Result =
new RedirectToRouteResult(new RouteValueDictionary
{ { "action", "Index" }, { "controller", "Login" }, { "Area", "" } /*, { "returnUrl", LastUrl }, { "Message", "Your session has been expired" }*/ });
return;
}

Answers (1)