I am using the following to validate my user which works but i wish to incoperate roles so I used the reg iis to add the standard tables aspnet_UsersInRoles.
What I basically want is the ability to have variables canView canDelete canEdit and that I can just access them when my dal is called.
protected void btnLogin_Click1(object sender, EventArgs e)
{
Users _loginUser = _dal.VerifyPassword(txtUserName.Text, txtPassword.Text);
if (failedLogin == false)
lblerror.Text = "Invalid Login";
else
{
UserData userData = new UserData
{
FirstName = _loginUser
};
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // ticket version
_loginUser.UserName, // authenticated username
DateTime.Now, // issueDate
DateTime.Now.AddMinutes(30), // expiryDate
isPersistent, // true to persist across browser sessions
userData, // can be used to store additional user data
FormsAuthentication.FormsCookiePath); // the path for the cookie
// Encrypt the ticket using the machine key
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
// Add the cookie to the request to save it
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);
lblerror.Text = "Success!";
Response.Redirect("default.aspx");
}
}
My Question is how would i adapt the following routine to inlcude the roles aspnet_UsersInRoles table to check if has administrative privellages Of a guid type of ED85788D-72DA-4D0A-8D5E-B5378FC00592.
public Users VerifyPassword(string userName, string password)
{
//The ".FirstOrDefault()" method will return either the first matched
//result or null
Users myUser = SoccerEntities.Users
.FirstOrDefault(u => u.UserName == userName
&& u.password == password);
if (myUser == null) //User was not found
{
//Proceed with your login process...
return myUser;
}
else //User was found
{
if (isPlayerdb(userName))
isPlayer = true;
if (isAdmindb(userName))
isAdmin = true;
return myUser;
//Do something to let them know that their credentials were not valid
}
}
Cause at min im doing this setting the isAdmin variable on a other function isAdmindb so thats three hits to db which is not very effiecent
protected bool isAdmindb(string userName)
{
try{
Users adminUsers = (from users in SoccerEntities.Users
where users.roleId == new Guid("ED85788D-72DA-4D0A-8D5E-B5378FC00592") && users.UserName == userName
select users).FirstOrDefault();
if (adminUsers != null)
return true;
else
return false;
}
catch (Exception ex)
{
throw new EntityContextException("isAdmin failed.", ex);
}
}
Edit a had a typo in my return !!!
VerifyPassword()
always returnsnull
? What's the point of that? \$\endgroup\$