-
-
Save ToJans/874211 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using System.Web | |
@using System.Web.Security | |
<style type="text/css"> | |
p,label {color:black;} | |
</style> | |
@{ | |
// just refactored things a bit | |
var flashmessage = Request["flash"]; | |
// POST | |
if (IsPost) | |
{ | |
var shouldRedirect = false; | |
if (Request["submit"]=="login") | |
{ | |
string username=Request["username"]; | |
string password=Request["password"]; | |
if (Membership.ValidateUser(username, password)) | |
{ | |
FormsAuthentication.SetAuthCookie(username, true); | |
flashmessage="Welcome, "+username; | |
} | |
else | |
{ | |
flashmessage="Login failed: wrong username or password"; | |
} | |
shouldRedirect=true; | |
} | |
if (Request["submit"]=="logout") | |
{ | |
FormsAuthentication.SignOut(); | |
flashmessage="Logged off"; | |
shouldRedirect=true; | |
} | |
if (shouldRedirect) | |
{ | |
var redirectUrl = Request.Url.PathAndQuery.ToString(); | |
if (!string.IsNullOrEmpty(flashmessage) | |
{ | |
redirectUrl += redirectUrl.Contains("&")?"?":"&"; | |
redirectUrl += "flash="+UrlEncode(flashmessage); // not sure how to call this urlencode function | |
} | |
Response.Redirect(redirectUrl); | |
} | |
} | |
// GET | |
<p>@flashmessage</p> | |
var currentUser = Membership.GetUser(); | |
if (currentUser==null) | |
{ | |
<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> | |
} | |
else | |
{ | |
<form method="post"> | |
<input type="submit" id="submit" name="submit" value="logout"/> | |
</form> | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment