utenticación de formularios comprensión context.user.identity
Dado que la documentación sobre este proceso es muy vaga y confusa (o antigua), quería verificar que lo estaba haciendo correctamente y que no faltaba ningún paso.
stoy tratando de crear un sistema de inicio de sesión seguro, que caduca al cerrar el navegador.
- en mi web.config tengo lo siguiente -
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" defaultUrl="Index.aspx" name=".ASPXFORMSAUTH" timeout="100" />
</authentication>
<authorization>
<allow users="?" />
</authorization>
<machineKey decryption="AES" validation="SHA1" validationKey.......... />
Así que tengo un formulario de inicio de sesión con un cuadro de texto de nombre de usuario / contraseña y este botón:
<asp:Button ID="LoginButton" runat="Server" OnClick="Login_Authenticate" Text="Sign in" />
Inside Login_Authenticate Hago lo siguiente:
protected void Login_Authenticate(object sender, EventArgs e){
string userName = UserName.Text;
string password = Password.Text;
bool Authenticated = false;
// Here's code that makes sure that Username and Password is CORRECT
if(AuthClass.Authenticate(userName, password)){
Authenticated = true;
}
// error checking does happen here.
if (Authenticated)
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(30), rememberUserName, String.Empty, FormsAuthentication.FormsCookiePath);
string encryptedCookie = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookie);
cookie.Expires = DateTime.Now.AddMinutes(30);
Response.Cookies.Add(cookie);
//FormsAuthentication.RedirectFromLoginPage(userName, false);
Response.Redirect("MainPage.aspx");
}
}
--- en MasterPage.master.cs tengo la siguiente comprobación en Page_Init () ---
if (Context.User.Identity.IsAuthenticated)
{
int userid = (int)Session["userid"];
if (userid == null)
{
userid = GetUserID(Context.User.Identity.Name);
if (userid != null)
{
Session["userid"] = userid;
}
}
}
EDIT: --- GLOBAL.ASAX; algún código que no estoy seguro es correcto o sé lo que hace
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
// look if any security information exists for this request
if (HttpContext.Current.User != null)
{
// see if this user is authenticated, any authenticated cookie (ticket) exists for this user
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
// see if the authentication is done using FormsAuthentication
if (HttpContext.Current.User.Identity is FormsIdentity)
{
// Get the roles stored for this request from the ticket
// get the identity of the user
FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
//Get the form authentication ticket of the user
FormsAuthenticationTicket ticket = identity.Ticket;
//Get the roles stored as UserData into ticket
string[] roles = { };
//Create general prrincipal and assign it to current request
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(identity, roles);
}
}
}
}
--- a partir de entonces, en cada página, uso el ID de usuario de sesión para recopilar la información y el contenido del usuario y asegurarme de que el usuario tenga la autenticación adecuada y los permisos de rol de grup
¿Es todo esto correcto? ¿O tengo que descifrar algo en alguna parte?
¿Es esto suficiente para hacer un inicio de sesión de usuario seguro? ¿O no debería molestarme con la autenticación de formularios y encontrar mi propia manera de hacer mis propias cookies y administrarlas yo mismo?