asp.net different view for logged user - asp.net

Let´s say, I have a button in my View. If the page is accesed by logged-in users it should be displayed normally. If it is accesed by non-loged (anonymous) user, I want the button not to be displayed.
The button visibility should not be affected by the user role, just by the fact if someone is logged in.
How can this be achieved ?

I have figured it out. This condition is sufficient for this purpose:
#if (User.Identity.IsAuthenticated)
{
<button>Send</button>
}

Related

How to prioritize firebase web auth before loading my web page

I have a simple login layout. I want to stop loading or displaying it when the user is logged in the web page. Everything is ok but there seems to be a slight delay when verifying firebase auth that's why whenever I refresh my web page, the login layout still displays very briefly though. I want it to not be seen totally.
Here is the video of my problem
As you can see, In the video the user is already logged in. But the username and password and button for login is still showing very briefly.
You don't share any code, but I make the assumption that you use the onAuthStateChanged() observer. As explained in the doc, here and here, the Auth object needs to initialize before the observer is triggered, therefore you cannot avoid the behaviour you experience.
You need to manage yourself the displaying as follows:
Before the Auth object is fully initialized (i.e. the Auth object is in an intermediate state), show a welcome/default page with spinner
If the user is already signed-in show the relevant page
If the user is not signed-in show the login page
Something along the following (pseudo commented code) lines:
// Show the spinner
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
// Hide the spinner and show content
} else {
// No user is signed in.
// Hide the spinner and show the login form
}
});

Trigger an event in GTM if the user is logged in?

I’m trying to trigger a tag (a button click) only when a user is logged in. So, if the user clicks the “write a review” button the event will trigger if the user is logged in, otherwise it won’t trigger.
In the source code log in data is indicated by this string:
<input type="hidden" id="isLoged" name="isLoged" value=“true”>
If the user is logged in the value return “true”, if not it returns “false”.
I’ve searched the web, tried many things but couldn’t find a way to do it.
Thanks in advance any help.
You can do it with a Custom JS variable:
function () {
return document.getElementById("isLoged").value;
}

ASP.net MVC3 save last page a user viewed to display when they return to site

My site uses Windows Live to authenticate users. The problem with this is that they are redirected away from the site to log in, and then back to my own SignIn action, which registers/signs in the user to my site. Then by default I have the SignIn action redirecting to the user's profile page.
But what I want instead is for the SignIn action to return them the last page they viewed. This seems like it should be simple.
My first thought was to add the following to my important actions to save the user's current place:
Session["LastPage"] = RedirectToAction("Index", "Home");
Then at the end of my SignIn action I tried to include:
if (Session["LastPage"] != null)
return (ActionResult)Session["LastPage"];
else
return RedirectToAction("MyPage", "User");
This doesn't actually work though, since apparently my session is getting restarted every time the user navigates away from and back to the site.
Am I approaching this issue the right way? If so, how do I keep the session from getting wiped? If I'm going about this completely the wrong way, then can someone point me in the right direction?
You should set the ReturnUrl property of WindowsLiveLogin object (assuming you're using one).
http://msdn.microsoft.com/en-us/library/cc287661.aspx

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.

How to check for valid user and then continue submitting data

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

Resources