Umbraco member login script (razor)
@using System.Web
@using System.Web.Security
@helper LoginForm()
{
<form method="post">
<div><label for="name">Username:</label>
<input type="text" id="username" name="username"/></div>
<div><label for="password">Password:</label>
<input type="password" id="password" name="password"/></div>
<div><input type="submit" id="submit" name="submit" value="login"/></div>
</form>
}
@helper LogoutForm()
{
<form method="post">
<input type="submit" id="submit" name="submit" value="logout"/>
</form>
}
@helper Message(string message)
{
<p>@message</p>
}
<style type="text/css">
p,label {color:black;}
</style>
@{
var isSubmitLogin = (IsPost && Request["submit"]=="login");
var isSubmitLogout = (IsPost && Request["submit"]=="logout");
var currentUser = Membership.GetUser();
var requestedUrl = Request.Url.PathAndQuery.ToString(); // Model.Url;
if (Request["ReturnUrl"]!=null)
{
requestedUrl = Request["ReturnUrl"];
}
if (currentUser!=null)
{
if (!isSubmitLogout)
{
@Message("Logged in : " + currentUser.UserName)
@LogoutForm()
}
else
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
}
if (currentUser==null)
{
if (!isSubmitLogin)
{
@LoginForm()
}
else
{
string username=Request["username"];
string password=Request["password"];
if (Membership.ValidateUser(username, password))
{
// RedirectFromLoginPage does not work that good within the Umbraco context
// FormsAuthentication.RedirectFromLoginPage(username, true);
FormsAuthentication.SetAuthCookie(username, true);
// Redirect to / refresh the requested page
Response.Redirect(requestedUrl);
}
else
{
@Message("Login failed for " + username)
@LoginForm()
}
}
}
}