Skip to content

Instantly share code, notes, and snippets.

@ErikHen
Created May 11, 2017 09:33
Show Gist options
  • Save ErikHen/ee44e4bf3cbfe92df31b9ed60bf9c495 to your computer and use it in GitHub Desktop.
Save ErikHen/ee44e4bf3cbfe92df31b9ed60bf9c495 to your computer and use it in GitHub Desktop.
Episerver Mixed-mode auth. Login controller
namespace MyNamespace.Controllers
{
public class LoginController : Controller
{
private UIUserProvider UIUserProvider => ServiceLocator.Current.GetInstance<UIUserProvider>();
private UISignInManager UISignInManager => ServiceLocator.Current.GetInstance<UISignInManager>();
public ActionResult Index()
{
return View("/Views/Shared/Login.cshtml");
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult Index(LoginViewModel model)
{
if (ModelState.IsValid)
{
var resFromSignIn = UISignInManager.SignIn(UIUserProvider.Name, model.Username, model.Password);
if (resFromSignIn)
{
return Redirect(UrlResolver.Current.GetUrl(ContentReference.StartPage));
}
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("LoginError", "Login failed");
return View("/Views/Shared/Login.cshtml", model);
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult AdfsLogin(string returnUrl)
{
if (ModelState.IsValid)
{
var properties = new AuthenticationProperties { RedirectUri = returnUrl };
HttpContext.GetOwinContext().Authentication.Challenge(properties, WsFederationAuthenticationDefaults.AuthenticationType);
}
return View("/Views/Shared/Login.cshtml");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment