PDA

View Full Version : How to use authentication mode="Forms" with ExtJS



Ex_Soft
26 Aug 2010, 11:40 PM
I have MainForm.aspx and LoginForm.aspx same as here (http://www.sencha.com/learn/Tutorial:Basic_Login). And


<authentication mode="Forms">
<forms loginUrl="LoginForm.aspx" />
</authentication>
in Web.config.
I looked for (http://www.google.com/#hl=en&q=extjs%2Basp.net%2Bauthentication%2Bmode%2Bforms) but didn't find anything. What I should do in LoginForm's handler?
For example in ASP.NET:


protected void ButtonLogin_Click(object sender, EventArgs e)
{
string
Login;

if ((Login = TextBoxLogin.Text.Trim()) == "SmthLogin" && TextBoxPassword.Text.Trim() == "SmthPasswd")
FormsAuthentication.RedirectFromLoginPage(Login, false);
else
LabelInfo.Text = "Login failed. Please check your user name and password and try again.";
}

Ex_Soft
27 Aug 2010, 2:04 AM
I used FormsAuthentication.SetAuthCookie() (http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.setauthcookie.aspx)


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;

using Jayrock.Json;

namespace TestFormAuthentication
{
public class LoginFormHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.CacheControl = "no-cache";
context.Response.Expires = -1;
context.Response.ContentType = "application/json";

string
loginUserName = context.Request.Form["loginUsername"],
loginPassword = context.Request.Form["loginPassword"];

bool
IsAuthenticationSuccess;

JsonObject
tmpJsonObject = (IsAuthenticationSuccess = (loginUserName == "SmthLogin" && loginPassword == "SmthPasswd")) ? new JsonObject(new Dictionary<string, object> { { "success", true } }) : new JsonObject(new Dictionary<string, object> { { "success", false }, { "errors", new Dictionary<string, object> { { "reason", "Login failed. Try again." } } } });

if (IsAuthenticationSuccess)
FormsAuthentication.SetAuthCookie(loginUserName, false);

JsonTextWriter
tmpJsonTextWriter = new JsonTextWriter(context.Response.Output);

tmpJsonObject.Export(tmpJsonTextWriter);
tmpJsonTextWriter.Flush();
tmpJsonTextWriter.Close();
}

public bool IsReusable
{
get
{
return false;
}
}
}
}