Podemos usar el conocido membership provider de ASP.NET en nuestras aplicaciones winform.
Cual es el goal? El membership de ASP.NET es realmente una vía completa par administrar usuarios y roles (Create,Edit,Delete, reglas de acceso a formularios, reglas de acceso a paginas ).
Como lo implementamos?
Primero creamos un formulario de login que se conecta a la base de datos ASPDBNET.mdf (generada automaticamente en un proyecto asp.net)
El codigo del boton login debe ser :
VB
Imports System.Security.Principal
Imports System.Web.Security
Private
Sub
OK_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
OK.Click
If
Membership.ValidateUser(UsernameTextBox.Text, PasswordTextBox.Text)
Then
'''' treatment if authenticated
Else
''''
End
If
End
Sub
C#
using
System.Security.Principal;
using
System.Web.Security;
private
void
OK_Click(System.Object sender, System.EventArgs e)
{
if
(Membership.ValidateUser(UsernameTextBox.Text, PasswordTextBox.Text)) {
}
// treatment if authenticated
else
{
//
}
}
Al mismo tiempo necesitamos configurar el app.config y agregar la siguiente configuración:
<
system.web
>
<
membership
defaultProvider
=
"AspNetSqlMembershipProvider"
>
<
providers
>
<
remove
name
=
AspNetSqlMembershipProvider
"/>
<
add
name
=
"AspNetSqlMembershipProvider"
type
=
"System.Web.Security.SqlMembershipProvider"
connectionStringName
=
"AspNetConnectionString"
enablePasswordRetrieval
=
"false"
enablePasswordReset
=
"true"
requiresQuestionAndAnswer
=
"false"
applicationName
=
"AspNet"
requiresUniqueEmail
=
"false"
passwordFormat
=
"Hashed"
maxInvalidPasswordAttempts
=
"5"
minRequiredPasswordLength
=
"7"
minRequiredNonalphanumericCharacters
=
"1"
passwordAttemptWindow
=
"10"
passwordStrengthRegularExpression
=
""
/>
</
providers
>
</
membership
>
<
roleManager
enabled
=
"true"
defaultProvider
=
"AspNetSqlRoleProvider"
cacheRolesInCookie
=
"true"
createPersistentCookie
=
"false"
cookieProtection
=
"All"
>
<
providers
>
<
add
name
=
"AspNetSqlRoleProvider"
type
=
"System.Web.Security.SqlRoleProvider"
applicationName
=
"AspNet"
connectionStringName
=
"AspNetConnectionString"
/>
</
providers
>
</
roleManager
>
</
system.web
>
Una vez autenticado y redireccionado a otro winform podemos detectar usuarios actuales y trabajar en las reglas de acceso por roles
VB
''' treatment for current user
nom = Thread.CurrentPrincipal.Identity.Name
Dim
user
As
MembershipUser = Membership.GetUser(nom)
Dim
identity
As
New
GenericIdentity(user.UserName)
Dim
principal
As
New
RolePrincipal(identity)
Threading.Thread.CurrentPrincipal = principal
If
Roles.GetRolesForUser(nom).Length = 1
Then
rolesArray = Roles.GetRolesForUser(nom)
role = rolesArray.First.ToString
''' enable controls for examples for the role of current user
Else
'' disable controls
End
If
C#
using
System.Security.Principal;
using
System.Web.Security;
public
class
AddUser
{
private
void
Button1_Click(System.Object sender, System.EventArgs e)
{
MembershipUser user;
user = Membership.CreateUser(TextBox1.Text, TextBox2.Text);
MsgBox(TextBox1.Text +
"is created successfully"
);
this
.Dispose();
}
}
Para crear un usuario usted puede crear un winform con simples textboxes (encriptado para el password) y usar ese codigo
VB
Imports
System.Security.Principal
Imports
System.Web.Security
Public
Class
AddUser
Private
Sub
Button1_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
Button1.Click
Dim
user
As
MembershipUser
user = Membership.CreateUser(TextBox1.Text, TextBox2.Text)
MsgBox(TextBox1.Text &
"is created successfully"
)
Me
.Dispose()
End
Sub
End
Class
C#
using
System.Security.Principal;
using
System.Web.Security;
public
class
AddUser
{
private
void
Button1_Click(System.Object sender, System.EventArgs e)
{
MembershipUser user;
user = Membership.CreateUser(TextBox1.Text, TextBox2.Text);
MsgBox(TextBox1.Text +
"is created successfully"
);
this
.Dispose();
}
}
No hay comentarios:
Publicar un comentario