-
-
Save 8SheldonS8/3841ba7de2dcd2712364c88df0533428 to your computer and use it in GitHub Desktop.
Static class to help with IIS Windows Authentication and 401
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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