How to check for valid user and then continue submitting data - asp.net

I am using ASP.NET to submit data from a form to a database.
But before submit I want to check if the current user us valid. If not I want to use the login control registration form where an existing user can enter his user id and password then go back to the previous page to submit the form data.
Example:
Assume a page having 3 textboxes and one button. Once the user clicks the button I want to check if the user is already logged in or not. If not I want to open the login page so he can provide his credentials and after that execute the previous page submit function without the user having to click again.

I think you're asking about form validation? The easiest way to do this is with a postback. Put a button on the form, that button will post back the form to itself, and fire the button_Click event. Inside that event you can put some C# code that will validate the user's name and password. Then you can show a label on the form that indicates the credentials are not correct.
Hope that helps!

You can save information about user, using session:
Session["UserLoggedIn"] = true;
When you want to check, if user is logged in, check like this:
if(Session["UserLoggedIn"] != null)
{
// User is logged in
}
else
{
// User is not logged in, so redirect to login page
Response.Redirect("~/login.aspx");
}
if you want to redirect back, after successful log in operation. you can use QueryString. So instead of:
Response.Redirect("~/login.aspx");
redirection will be:
Response.Redirect("~/login.aspx?RedirectUrl=your current form url");
In login page, you can read QueryString:
string redirectUrl = Request.QueryString["RedirectUrl"];
After login operation, use redirectUrl value, to redirect back:
Response.Redirect(redirectUrl);

i hope i have tried to understand you.
firstly, i would like to explain what you want.
You want to check user logged in before submit, and if he is not logged in you want him to redirect to login page after he logs in u want to come back on the same page. and perform your click function.
RetrunUrl
return url in your querystring will help to return to back to your original page. and u can save the details of the previous page in session and once user logs in and redirects back to original back perform your click event there. by validating you session value,
once you are done with your whole process remember to abandon your current session details

Related

Force anonymous to register before submitting a form. Drupal 7

I want to force anonymous to register an account before submitting the form.
So I have to allow permission for anonymous to access the form,
Then I use hook_form_FORM_ID_alter to edit form. Then I wanted to redirect submit button to another link if it is clicked by anonymous.(I still have no idea how to do it. It would be nice if anyone can tell me)
Is this the right solution?
For now,
I have a pop-up a login form (Modal forms and Facebook OAuth). In the form there is a register button. Then If user choose to register I want to keep the form that he have already input and show it after he confirm his email.
Thank you.
Off the top of my head, I would allow the form to be submitted either by an anonymous user or a registered one.
in my function MYMODULE_MYFORM_submit() I would check to see if the user is logged in.
If yes proceed as normal, if no, store the form, either in a temp SESSION variable or into a custom MySQL table and forward the user to login/register page (mysite.com/user) using drupal_goto('user')
once the user was registered you could then check for the existence of the form in the SESSION array or your MySQL table, and then carry on with the process as you would if the user had been logged in in the first place
Hope this makes sense or is of help

Going back to previous page after page redirection

Hi I am tring to send a user to the login page if he is not authetificated and after he logs in I want to send him back to the page he was the problem is I am getting only half way to the page I want the user to be.Here is my code:
When I user clicks a button in my application I check if the user is logged in if he is not.I refirect him to the loggin page with this protion of code:
return RedirectToAction("LogIn" , "Account" , new { returnUrl = "Products" , subCat = Request.QueryString["subcat"]});
What I would expect to get out of this code is:
LogIn?returnUrl="Products"&subCat="Notebooks"
What I get is this:
Products?subcat=Notebooks
This is the link from the page I cliked the button from.Now this behavior may be because the links are all created using ajax.ActionLink but if that is true how can I get back to my previous page?
I have not modified the default routing of the MVC application.
Set the returnUrl like this returnUrl = ViewContext.HttpContext.Request.Url.PathAndQuery and leave the subcat alone.

Remember login from WordPress password protected page

I know how to password protect a page and check if it has been entered with post_password_required. What I need to know is if I can then continue to know if a user has logged into this password protected page even after leaving it. The reason for this is because I would like to display a site wide navigational sidebar that allows the user to get back to that page, but only have it show if they have logged into that page earlier.
Set a session variable if they login successively to the given page, then when they are on another page later, have the sidebar echo the link if that session var is set.
The value of the session var can be the url to create the link, if you have more than one page you're trying to do this with.

Drupal: send output to user in form's submit handler, instead of redirecting

I have this basic Drupal scenario and question:
I have a form which accepts some input from user, and a submit handler which should process data and show the result to user. In other words, I don't write anything in database or set e variables etc., just show some output to user.
I was wondering how I can do this, because a submit handler redirects the flow to another menu item -which the form values ($form_state) are not available anymore. Redirecting form to itself is not useful, because I just receive the raw posted inputs -not processed $form_state.
How can I prevent the redirection and just show some output to user in submit handler?
Thank you.
When you are building your form you want to set re-direct to false:
$form['#redirect'] = FALSE;
There is more on redirect here: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#redirect
If I have understood your question correctly this is what you are after.

ASP.NET page redirection / crosspost

I have 3 asp.net pages: Search.aspx, Results.aspx and Login.aspx.
Now I want anonymous users to be able to search, so everyone can use search.aspx. This page calls
Server.Transfer(Results.aspx)
and therefore shows the results. Now when the user is not logged in, a link to the login page is displayed on the Results page. The problem is:
After the login, I want the user to be redirected automatically to the Results page. However I do not succeed so, as the PreviousPage property of Login.aspx is always null. And when I use
Request.UrlReferrer.LocalPath
it's the link to Search.aspx but not Results.aspx.
Also, when the user is on the Results page, how do I enable him to go back by clicking a link and all his search input criteria (like in textboxes) on the Search.aspx is still there so he can refine the search after having seen the results? Whenever I send the user back, all user input is lost.
I still haven't figured out if I should use a normal hyperlink, a linkbutton and how to retrieve the previous page url or preserve the input data.
I can use AJAX if that is any help, but a "pure" asp.net solution would be preferred.
When you do a Server.Transfer it is a serverside redirect...meaning the client still sees the original URL. You need to post the search criteria to the results page, store them locally, show the login link. When they are logged in you can redirect to the results page and rehydrate the search critera and show the results.
Try using Response.Redirect("url") instead of Server.Transfer. When you use Server.Transfer, the url on the client will remain the Search page and not actually redirect to the Results.
You can use User.Identity.IsAuthenticated to check if the user is logged in and show/hide the login button based on that.
To keep values for the page, you could store them in Session, then have the Search page look for them and, if they exist, place them in the controls.
You can also embed the URL you want to return to after login into the querystring if you want.

Resources