Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@dotnetwise dotnetwise commented Jun 3, 2012

Why no register / change password pages?

@stal1989

This comment has been minimized.

Copy link

@stal1989 stal1989 commented Oct 18, 2012

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

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

@fontanka16

This comment has been minimized.

Copy link

@fontanka16 fontanka16 commented Dec 20, 2012

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

This comment has been minimized.

Copy link

@benmarte benmarte commented Jul 22, 2014

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