Skip to content

Instantly share code, notes, and snippets.

@joeriks
Created March 17, 2011 11:44
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joeriks/874194 to your computer and use it in GitHub Desktop.
Save joeriks/874194 to your computer and use it in GitHub Desktop.
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()
}
}
}
}
@dotnetwise
Copy link

Why no register / change password pages?

@stal1989
Copy link

pls explain more about login and logout coding.......

if we want to redirect page.. how it is possible.....

@fontanka16
Copy link

provided membership is configured with requiresUniqueEmail="true" the following should allow for users signing in with username OR email:

string username=Membership.GetUserNameByEmail(Request["username"])?? Request["username"];

Do you agree?

@benmarte
Copy link

Does this work with Umbraco 7? I keep getting a Error loading MacroEngine script (file: RazorLogin.cshtml) when inserting the macro in the RTE.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment