I am trying to track my login attempts but its only ever staying at one I need keep a running track of failed login attempts probably something simple wrong with my execution here.
public int validateUser(string username, string userPassword)
{
var _currentUser = _db.users.FirstOrDefault(a => a.username == username);
int _loginAttempts = =0;
if (_currentUser != null)
{
if (isEncryptionEnabled == true)
{
string descriptedPassword = encrypt.DecryptRijndael(_currentUser.password, _currentUser.salt.ToString());
if (descriptedPassword == userPassword)
return 0;
else
_loginAttempts++;
}
else
{
var _unecryptedUser = _db.users.FirstOrDefault(a => a.username == username && a.password == userPassword);
if (_unecryptedUser != null)
return 0;//zero represents sufcessfull login attempts
return _loginAttempts++; // if anything greater zero is a failed login attempt send it back.
}
}
return _loginAttempts;
}
This is how the function is being called a button click of Login.aspx this is a webforms application.
protected void btnLogin_Click(object sender, EventArgs e)
{
DBContext _db = new DBContext();
int canILogin = _db.validateUser(txtUsername.Text, txtPassword.Text);
if (canILogin == 0)// if i can loging after checking the encripted password then lets go to default page
Response.Redirect("default.aspx");
else
Response.Redirect("login.aspx?attempt=" + canILogin.ToString());
if (Convert.ToInt16(Request.QueryString["attempt"]) > 0)
{
lblFailed.Text = "Failed Login Attempts"+ canILogin.ToString();
lblFailed.Visible = true;
}
}
grovesNL is correct. Move your declaration for_loginAttempts outside the method. Try declaring it in the parent class. Like this:
class DBContext
{
private int _loginAttempts = 0; // I assume private to keep from exposing it for security
public int validateUser(string username, string userPassword)
{
// some code to test userPassword and increase or reset counter as needed
}
// more methods etc.
}
Related
In my Xamarin.Forms project I have a login form that logs in the user and then redirects them to another page. I want to show an ActivityIndicator while it attempts to login, but setting IsVisible to true doesn't actually take effect before the login function is done. My code looks like this:
void OnLoginButtonClicked(object sender, EventArgs e)
{
LoadingIndicator.IsVisible = true;
Login();
}
public void Login()
{
var user = new User
{
Email = usernameEntry.Text,
Password = passwordEntry.Text
};
User validUser = AreCredentialsCorrect(user);
if (validUser != null)
{
Navigation.PushAsync(new ProfilePage());
}
else
{
messageLabel.Text = "Login failed";
passwordEntry.Text = string.Empty;
//It will only show the LoadingIndicator at this point.
}
}
If the user is correct, it never shows the LoadingIndicator because it navigates to another page before it can show it.
If the user is invalid, it will only show the LoadingIndicator after it hits the else clause and shows the "Login failed". Can anyone figure why that is, and what I can do to fix it?
Use Async calls and await the main block of operations. If anything you want to update/change in UI, do it using BeginInvokeOnMainThread.
void OnLoginButtonClicked(object sender, EventArgs e)
{
LoadingIndicator.IsVisible = true;
await Login();
}
public async void Login()
{
await Task.Run(() => {
var user = new User
{
Email = usernameEntry.Text,
Password = passwordEntry.Text
};
User validUser = AreCredentialsCorrect(user);
}).ContinueWith((a) => SomeMethod(validUser));
}
public void SomeMethod()
{
Device.BeginInvokeOnMainThread(() =>
if (validUser != null)
{
Navigation.PushAsync(new ProfilePage());
}
else
{
messageLabel.Text = "Login failed";
passwordEntry.Text = string.Empty;
//It will only show the LoadingIndicator at this point.
}
}
}
Try using async/await. Allow the UI to update while also navigating.
async void OnLoginButtonClicked(object sender, EventArgs e)
{
LoadingIndicator.IsVisible = true;
await Login();
}
public async Task Login()
{
var user = new User
{
Email = usernameEntry.Text,
Password = passwordEntry.Text
};
User validUser = AreCredentialsCorrect(user);
if (validUser != null)
{
await Navigation.PushAsync(new ProfilePage());
}
else
{
messageLabel.Text = "Login failed";
passwordEntry.Text = string.Empty;
//It will only show the LoadingIndicator at this point.
}
}
Two possible explanations here one it could be redirecting so fast that it doesn't have time to display the loadingindicator, I have experienced this problem before. We had a really nice loading symbol and animation but then we switched to Aurelia framework and it logs in so fast it just doesn't have time to display it even though it was actually working. As for code changes I would try to add it into the login function as well at least for right now to give some clarity to if its actually just logging in so fast its not displayed or its just not displaying at all. Her is my suggestion.
void OnLoginButtonClicked(object sender, EventArgs e)
{
LoadingIndicator.IsVisible = true;
Login(LoadingIndicator.IsVisible);
}
public void Login(Bool IsVisible)<--- might have type wrong not familiar with you custom defines types I would expect it to be a bool though.
IsVisible = true;
{
var user = new User
{
IsVisible = true;
Email = usernameEntry.Text,
Password = passwordEntry.Text
};
User validUser = AreCredentialsCorrect(user);
if (validUser != null)
{
IsVisible = true;
Navigation.PushAsync(new ProfilePage());
}
else
{
IsVisible = true;
messageLabel.Text = "Login failed";
passwordEntry.Text = string.Empty;
//It will only show the LoadingIndicator at this point.
}
}
If nothing else this might help clarify why it isn't being displayed. Also don't forget to use the web debugger f12 by default on most browsers and look for the element that will contain the Indicator.
Hope this helps! If not let me know and I'll remove the answer(I had to use an answer because I can't comment under 50 rep) Cheers!
I have website hosted over here: but when I login then the url into the address bar keeps repeating and getting longer like: this and here if you click on to the left side menu then you can see into the address bar that the URL keeps getting bigger and bigger. Please suggest me on the above why the URL is getting bigger like this?
please find the website credential as below:
username: int123
password: 123
Here is code sample:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["type"] == "logout")
{
Session.Clear();
Response.Cookies.Clear();
//FormsAuthentication.SignOut();
Response.Redirect("http://103.252.236.33/plesk-site-preview/2wayglobal.com/103.252.236.33/login.aspx");
}
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
string username = string.Empty;
string userid = string.Empty;
string address = string.Empty;
string company = string.Empty;
if ((txtUsername.Value.Trim() == string.Empty || txtUsername.Value.Trim() == "Username") && (txtPassword.Value.Trim() == string.Empty || txtPassword.Value.Trim() == "Password"))
{
//divError.Visible = true;
lblError.Text = "Please type the correct username and password";
ResetFields();
}
else if (txtUsername.Value.Trim() == string.Empty || txtUsername.Value.Trim() == "Username")
{
//divError.Visible = true;
lblError.Text = "User name is incorrect.";
ResetFields();
}
else if (txtPassword.Value.Trim() == string.Empty || txtPassword.Value.Trim() == "Password")
{
//divError.Visible = true;
lblError.Text = "Password is incorrect.";
ResetFields();
}
else
{
DataTable dtuserLogin = db.GetLoginDetails(txtUsername.Value.Trim(), txtPassword.Value.Trim());
try
{
if (dtuserLogin.Rows.Count > 0)
{
userid = Convert.ToString(dtuserLogin.Rows[0]["srno"]).Trim();
username = Convert.ToString(dtuserLogin.Rows[0]["username"]).Trim();
company = Convert.ToString(dtuserLogin.Rows[0]["company"]).Trim();
address = Convert.ToString(dtuserLogin.Rows[0]["address"]).Trim();
//FormsAuthentication.SetAuthCookie(username + ";" + company + ";" + address, true);
}
}
catch (Exception ex)
{
throw new Exception("Error in btnLogin_Click()" + ex.Message);
}
if (username == "admin")
{
Response.Redirect("http://103.252.236.33/plesk-site-preview/2wayglobal.com/103.252.236.33/Admin/Default.aspx", true);
}
else
{
Response.Redirect("http://103.252.236.33/plesk-site-preview/2wayglobal.com/103.252.236.33/User/Default.aspx", true);
}
}
}
private void ResetFields()
{
txtUsername.Value = "Username";
txtPassword.Value = "Password";
}
It is just the code at the end of the btn click event that redirects you to that url:
if (username == "admin")
{
Response.Redirect("http://103.252.236.33/plesk-site-preview/2wayglobal.com/103.252.236.33/Admin/Default.aspx", true);
}
else
{
Response.Redirect("http://103.252.236.33/plesk-site-preview/2wayglobal.com/103.252.236.33/User/Default.aspx", true);
}
I can't see the code that is causing the issue, but I will bet $1 you are calling Redirect and passing what you think is an absolute URL but is actually a relative URL. The browser is seeing the redirect as relative and simply adding on the path to the end of the URL that is already in the address bar. It will do this over and over until the URL overflows its buffer and you get a Bad Request error.
Instead of
Response.Redirect("AbsolutePath/PageName.aspx") //This is a relative URL!
you should use
Response.Redirect("~/AbsolutePath/PageName.aspx")
or
Response.Redirect("/AbsolutePath/PageName.aspx")
or
Response.Redirect("https://ServerName.com/AbsolutePath/PageName.aspx")
...depending on what you are trying to accomplish.
Every time I try to Response.Redirect("tothepageIwant.aspx"); tt takes me to ~/Account/Logon.aspx
Why is this happening? I'm using Forms Authentication, with a custom method of authenticating, using PrincipalContext.ValidateCredentials.
If the credentials are valid, I want to Redirect.Response to the page I'm allowing the user to reach.
Instead, anytime I successfully login, it redirects me to the old Account/Logon.aspx.
Any suggestions? Anything I need to look out for when using Forms Authentication with custom method of authenticating?
EDIT (add code):
protected void Submit1_Click(object sender, EventArgs e)
{
var auth = new AuthClass();
var result = auth.ValidateCredentials(UserEmail.Text, UserPass.Text);
if (result)
{
Response.Redirect("~/Members/RollReport.aspx");
}
else
{
Msg.Text = "Not authorized to access this page.";
}
}
public bool ValidateCredentials(string user, string pass)
{
using (var pc = new PrincipalContext(ContextType.Domain, "Domain.name"))
{
// validate the credentials
try
{
var isValid = pc.ValidateCredentials(user, pass);
if (isValid)
{
var isAuth = AuthorizeUser(user);
return isAuth;
}
else
{
return false;
}
}
catch (ActiveDirectoryOperationException)
{
throw;
}
}
}
private bool AuthorizeUser(string user)
{
var isAuth = false;
var authList = (List<string>)HttpContext.Current.Cache["AuthList"];
foreach (var id in authList)
{
if (id == user)
{
isAuth = true;
}
}
return isAuth;
}
var userName = Request.ServerVariables["LOGON_USER"];//or some other method of capturing the value from the username
var pc = new PrincipalContext(ContextType.Domain);
var userFind = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, userName);
if(userFind != null)
{
HttpContext.Current.Session["username"] = userFind.DisplayName;
}
If you want to check and redirect.. store the value inside a session variable inside the Global.asax
protected void Session_Start(object sender, EventArgs e)
{
//declare and Initialize your LogIn Session variable
HttpContext.Current.Session["username"] = string.Empty;
}
On the Page_Load of your login page assign the value if the code above succeeds
if(HttpContext.Current.Session["username"] == null)
{
//Force them to redirect to the login page
}
else
{
Response.Redirect("tothepageIwant.aspx");
}
if you want to do the same thing inside a using(){} statement
string fullName = null;
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
using (UserPrincipal user = UserPrincipal.FindByIdentity(context,"yourusernamehere")) //User.Identity.Name
{
if (user != null)
{
fullName = user.DisplayName;
}
}
}
use the debugger and inspect all of the user. Properties ok
I have a problem with my async method in WPF application. I want to implement asynch await pattern in my WPF application to retain form responsiveness when querying database with EntityFramework. I did everything as described in microsoft examples but it doesn't return control flow to the application as example applications do.
Here is my button click code:
private async void LoginButton_Click(object sender, RoutedEventArgs e)
{
var loggedIn = await _gluUserRepository.LoginAsync(LoginTextBox.Text, PasswordTextBox.Text);
Switcher.Switch(new Loader());
if (loggedIn)
{
UserName = LoginTextBox.Text;
Switcher.Switch(new Blank());
}
else
{
UserName = String.Empty;
MessageBox.Show("Błędny login lub hasło. Spróbuj ponownie.");
Switcher.Switch(new Login());
}
}
Here is my LoginAsync method:
public async Task<bool> LoginAsync(string login, string password)
{
string hashedPassword = PasswordHasher.Hash(password);
var user = await _dbContext.Users.FirstOrDefaultAsync(x => x.UserName == login);
if (user != null && user.Password == hashedPassword)
return true;
return false;
}
I've seen the same usage of async/await in Microsoft example applications, however, their WPF apps return control to Window handle, i.e. I can move window around when in my app that is impossible.
What I want to achieve is using async/await pattern with WPF application to retain responsiveness of application. I want to display loading circle when querying database and return to method when querying has been completed.
Anyone has an idea what am I doing wrong?
If I understand correctly
Switcher.Switch(new Loader());
is what shows the 'loading circle'.
You need to call it before you await the login. As it is now, your handler does everything else after LoginAsync returns.
You may also want to look at ReactiveUI. It provides a useful framework solving your problem.
It's hard to make guesses, but try changing the beginning of your LoginButton_Click code to start the task and await for its result later, like this:
private async void LoginButton_Click(object sender, RoutedEventArgs e)
{
var loggedInTask = _gluUserRepository.LoginAsync(LoginTextBox.Text, PasswordTextBox.Text);
MessageBox.Show("before await");
var loggedIn = await loggedInTask;
MessageBox.Show("after await");
// ...
}
You should be seeing two message boxes, one after another, both of them should be responsive, i.e. moveable and click-able. If you do see such behaviour, then the problem is most likely with your Switcher.Switch and has nothing to do with asynchronous methods of EntityFramework.
I've found solution. Though I don't know why it actually works. But it doesn't block UI thread. I don't think it's thread safe too, you should we weary.
private async void LoginButton_Click(object sender, RoutedEventArgs e)
{
var loginTask = userRepository.LoginAsync(LoginTextBox.Text, PasswordTextBox.Password);
controller.DisplayPageLoader();
DbUser loginResult = await loginTask;
if (loginResult != null)
{
controller.DisplayPageNewMeal();
controller.SetLoggedUser(loginResult);
}
else
{
MessageBox.Show("Błędny login lub hasło. Spróbuj ponownie.");
controller.DisplayPageLogin();
}
}
and then in repository
public Task<DbUser> LoginAsync(string login, string password)
{
return Task.Run<DbUser>( () => Login(login, password));
}
private DbUser Login(string login, string password)
{
try
{
string hashedPassword = PasswordHasher.Hash(password);
var user = _dbContext.Users.FirstOrDefaultAsync(x => x.UserName == login);
if (user.Result != null && user.Result.Password == hashedPassword)
return user.Result;
return null;
}
catch(Exception ex)
{
_logger.Error("Blad logowania uzytkownika", ex);
return null;
}
}
i have main form with async load method :
private async void MainMenu_Load(object sender, EventArgs e)
{
await _connection.Start();
await _myHub.Invoke("Join");
_myHub.On("recieved", data =>
{
var obj = JsonConvert.DeserializeObject(data);
messagewindows = new SendMessage(User, obj.Sender);
if ((obj.Reciever == User ) )
{
messagewindows. txtHistory.Text += obj.Sender + " :" + obj.text + Environment.NewLine;
messagewindows.Show();
}
});
}
i show second form for show message but form hang crash app :
private async void SendMessage_Load(object sender, EventArgs e)
{
Text = Reciever;
await _connection.Start();
await _myHub.Invoke("Join");
_myHub.On("recieved", data =>
{
var obj = JsonConvert.DeserializeObject<MessageSent>(data);
if ((obj.Reciever == Sender || obj.Sender == Sender) && (obj.Sender == Reciever || obj.Reciever == Reciever))
{
txtHistory.Text += obj.Sender + " :" + obj.text + Environment.NewLine;
txtHistory.SelectionStart = txtHistory.Text.Length;
txtHistory.ScrollToCaret();
}
});
}
I have a Boat Mooring reservations program, and currently, the way it works is, after you enter your name and click a button, the program gives you the next available mooring, there are 6 piers and 5 moorings per pier.
So, after all the moorings and piers are used up, the program crashes becuase there is nowhere for it to go, how can I make it so that the user gets a message box telling them, that there are no more spots available?
This is my code:
Button click:
private void button1_Click(object sender, EventArgs e)
{
var req = new BoatRequest();
req.Name = txtName.Text;
var client = new BoatReserveSoapClient();
BoatResponse response = client.ReserveMooring(req);
if (req != null)
{
Pierlbl.Text = response.Pier.ToString();
Mooringlbl.Text = response.Mooring.ToString();
Pierlbl.Visible = true;
Mooringlbl.Visible = true;
}
else
{
Pierlbl.Text = "Error Occured, please try again";
Mooringlbl.Text = "Error Occured, please try again";
}
}
The web method:
//Setting the max value for Piers and Moorings
public const int maxPiers = 6;
public const int maxMoorings = 30;
private static bool[,] reserveMooring = new bool[maxPiers, maxMoorings];
[WebMethod]
public BoatResponse ReserveMooring(BoatRequest req)
{
var res = new BoatResponse();
//if pierCalc set to 0, if smaller than maxPiers increment
for (int pierCalc = 0; pierCalc < maxPiers; pierCalc++)
{
//if mooringCalc set to 0, if smaller than maxMoorings increment
for (int mooringCalc = 0; mooringCalc < maxMoorings / maxPiers; mooringCalc++)
{
if (!reserveMooring[pierCalc, mooringCalc])
{
reserveMooring[pierCalc, mooringCalc] = true;
res.Pier = (Pier)pierCalc;
res.Mooring = (Mooring)mooringCalc;
return res;
}
}
}
return null;
}
This is where it crashes:
Pierlbl.Text = response.Pier.ToString();
Check that the response is not null, like this:
if (response != null)
{
Pierlbl.Text = response.Pier.ToString();
Mooringlbl.Text = response.Mooring.ToString();
Pierlbl.Visible = true;
Mooringlbl.Visible = true;
}
else
{
Pierlbl.Text = "Error Occured, please try again";
Mooringlbl.Text = "Error Occured, please try again";
}
You should just check if response != null and if it's the case, show the user a message.
In this example, req is never going to equal null. the response object is receiving the return value from your Web Service and would therefore the one to check for a null value in your if statement.
Like so:
if (response != null)
{
//Logic for success
}
else
{
//Logic for failure
}