Skip to content

Instantly share code, notes, and snippets.

@8SheldonS8
Last active August 6, 2016 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 8SheldonS8/3841ba7de2dcd2712364c88df0533428 to your computer and use it in GitHub Desktop.
Save 8SheldonS8/3841ba7de2dcd2712364c88df0533428 to your computer and use it in GitHub Desktop.
Static class to help with IIS Windows Authentication and 401
public ActionResult ChangeLogin(string userGuid = "")
{
if (String.IsNullOrEmpty(userGuid))
{
userGuid = Guid.NewGuid().ToString();
return RedirectToAction("ChangeLogin", new { userGuid });
}
else
{
Response.StatusCode = Security.StatusKeeper.Instance.GetCode(userGuid);
}
if (Response.StatusCode == 200)
{
return RedirectToAction("Index");
}
else
{
return View("Index");
}
}
public class StatusKeeper
{
private static StatusKeeper instance;
private Dictionary<string, int> currentCodes = new Dictionary<string, int>();
private Dictionary<string, int> postCounts = new Dictionary<string, int>();
private StatusKeeper() { }
public static StatusKeeper Instance
{
get
{
if(instance == null)
{
instance = new StatusKeeper();
}
return instance;
}
}
public int GetCode(string userGuid)
{
userGuid = userGuid.ToLower();
int httpCode = GetStatusCode(userGuid);
if(httpCode == 200)
{
RemoveUserGUID(userGuid);
}
return httpCode;
}
private int GetPostCount(string userGuid)
{
int yourCount = 0;
if (postCounts.ContainsKey(userGuid))
{
yourCount = postCounts[userGuid] + 1;
postCounts[userGuid] = yourCount;
}
else
{
yourCount = 1;
postCounts.Add(userGuid, yourCount);
}
return yourCount;
}
private int GetStatusCode(string userGuid)
{
int yourCount = GetPostCount(userGuid);
int yourCode = 200;
if (currentCodes.ContainsKey(userGuid))
{
yourCode = currentCodes[userGuid];
if (yourCode == 401 && yourCount > 2)
{
yourCode = 200;
yourCount = 0;
}
else
{
yourCode = 401;
}
currentCodes[userGuid] = yourCode;
}
else
{
currentCodes.Add(userGuid, 401);
yourCode = 401;
}
postCounts[userGuid] = yourCount;
return yourCode;
}
private void RemoveUserGUID(string userGuid)
{
currentCodes.Remove(userGuid);
postCounts.Remove(userGuid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment