I have an intranet module that requires a successful windows authentication to open the Index page, then based on the DOMAIN\USERNAME (using HttpContext) I get the role in a SQL table.
Currently for the roles I'm using the basic RoleProvider template to use [Authorize(Roles = "admin,customer")] for each page in the HomeController, which works in the forms authentication mode (had a basic username/password form), but not in the windows mode because I end up with a eternal login loop, I tried a bunch of SO solutions and this is how my WebConfig file looks like:
<system.webServer>
<modules>
<remove name="FormsAuthenticationModule" />
<remove name="FormsAuthentication" />
</modules>
</system.webServer>
<system.web>
<customErrors mode="Off" />
<sessionState timeout="1"></sessionState>
<authentication mode="Windows">
<forms loginUrl="~/Account/Login" protection="All" timeout="5"></forms>
</authentication>
<roleManager defaultProvider="roleProv" enabled="true">
<providers>
<clear />
<add name="roleProv" type="proj.classCS.roleProvider" enabled="true" />
</providers>
</roleManager>
The code also checks timeout in the session to redirect to the Login page on inactivity, both redirect to the index page but based on role menu items are different...
[HttpPost]
public ActionResult Login(usuario u, string retornaUrl)
{
//USER PRESS LOGIN BUTTON
//WINDOWS AUTH PROMPT
//IF WINDOWS AUTH IS SUCCESSFULL
//GET WINDOWS USER DOMAIN
string userDomWin = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name.ToString().Substring((HttpContext.User.Identity.Name.ToString().IndexOf("\\")) + 1);
string userWin = userDomWin.Replace("DOM\\", "");
//VALIDATE IF NETWORK USERNAME EXISTS ON SQL TABLE
var userNet = (from d in db.user
where d.userNet == userWin
select d.userNet).FirstOrDefault();
if (userNet != null)
{
//FormsAuthentication.SetAuthCookie(u.userNet, false);
Session["uname"] = userNet.ToString();
if (userNet != null)
{
return Redirect("~/Home/Index");
}
else
{
//CONN TIMEOUT / LOGOUT
TempData["Message"] = "SESSION TIMEOUT";
return Redirect("~/Account/Login");
}
}
else
{
//ON ERROR REDIRECT TO LOGIN PAGE WITH MESSAGE
TempData["Message"] = "TRY NEW CREDENTIALS";
return Redirect("~/Account/Login");
}
return View();
}