how can i authenticate an html control in a .aspx page - c#

how can i authenticate an html control in a .aspx page.
For eg. I have two labels :: lblusername and lblpassowrd, two textboxes to fetch the values from the user and a submit button.
I want all these controls to be of html controls not the asp.net server side controls, authenticate whether the password already exists, check for password string to be ok and on submit give user the home page.
any code help for this.
would also expect some discussion on the events fired, effect on the asp.net page life cycle and some insight about the performance.

Once the HTML controls have the runat="server" attribute, you just use the normal ASP.NET way of authenticating and authorizing.
http://msdn.microsoft.com/en-us/library/ms178329.aspx
http://msdn.microsoft.com/en-us/library/f8kdafb5%28VS.71%29.aspx

Related

maintain login and logout sessions in asp.net web forms

I am trying to implement login and logout in asp.net(web forms). In my web form I have two pages namely Default and Main. From Default page when I login with username and password it redirects to the Main page. When I press back button it directly redirects to the default page. For this I copied javascript code to my default page
<script type = "text/javascript" >
function preventBack() { window.history.forward(); }
setTimeout("preventBack()", 0);
window.onunload = function () { null };
source from stackoverflow question
After login when I click on back button in my browser(chrome) first it shows the Default page and then it shows the Main page. i.e page blinks when I click the back button.
It shows main page successfully with the issue.
What should I implement to stop showing the Default page when I click on back button
Update:
<script type = "text/javascript" >
history.pushState(null, null, 'Default.aspx');
window.addEventListener('popstate', function (event) {
history.pushState(null, null, 'Default.aspx');
});
</script>
I placed this code in default page
There is one important thing that you ought to know here.
One cannot disable the browser back button functionality only thing that can be done is prevent it.
You can't, in anyway diasble that button. What you can however do, is to put some logic and prevent that button from doing what it is meant to do.
Now for the script that you have shown, it should serve fine and the other thing to try here is to put a mediator page between your default and main page. So when you login, the control will flow to mediator page and it will then redirect to main page. Now when the user presses back button on the main page, the control will flow to mediator page which will again redirect the user to his main page.
The effect will be the same as your script, but putting a page can help you write Session handling code and some server side checks if you want.
But one thing is sure, the browser back button will be as it is.
Hope this helps.
As Matt said you can't disagree the back behaviour. However you can check if the user is logged in and redirect them to the main page easily.
All you need to do is, on the default page check if the user is logged in, if they are then redirect them to the main page, this way even if the user clicks back, they will be taken back to the main page. And also the can go to the Default page after logging out.
Another way could be using location.replace("...."), which replace the existing document and user can't "go back" using back button as the page doesn't exist in the url history.
Src: https://developer.mozilla.org/en-US/docs/Web/API/Location/replace

ASP.NET Page Cycle Confusion

If I have a button on my ASP.NET page which will take you to another page but that page will do something and then will send user back using
Uri uu= Request.UrlReferrer;
if (uu!= null)
Response.Redirect(uu.ToString());
Now Which page event can I use so that when other pages displays I can display a message box.
In short I am running my custom code in a "aspx" page where user is directed on button click, and then after custom code I am sending user back to old page, but it happens so quickly that user doesn't realize that he went on another page, now I want to display a message box after redirect on same page user started from, what to do :S !
More Information
EDIT
Sorry guys but I can't make changes to ASP page where button is at all
:(
I'm a bit unsure about how the Request.UrlReferrer gets set. I think it's a browser implementation detail. So I wouldn't trust on that.
I would go for something like
A.aspx -> Redirects to -> B.aspx
B.aspx -> Redirects to -> B.aspx?message=1
And check if message=1 is set.
But if you want to use the Request.UrlReferrer it should be accessible on Page_Load
If you use it this way, it'll never appear to the client.
Maybe try redirecting back using javascript with a delay so user can be informed
The best thing to do it's add a flag to the redirected page so you can show something special when the flag is turned on
Uri uu= Request.UrlReferrer;
if (uu!= null)
Response.Redirect(uu.ToString() + "?Message=DataHasChanged");
and then in the ASP page
<% if (Request.QueryString["Message"] == "DataHasChanged") { %>
<div class="alert">The data has changed. Please review it or whatever</div>
<% } %>

Autopostback in c# code while keeping several textboxes and dropdownlists values

I have several textboxesand dropdownlists on my form. When a user submits the form, I want to keep several of these values on the form but I also want to make them invisible for several seconds after the form submits, so the user knows the form has been submitted. I am pretty sure you need to do an autopostbackin order to change the visibility of textboxesand dropdownlists but I do not want to do an autopostback because I will lose the fields I want to keep. Any alternatives?
Can you please expand your question, give more light to what you're dealing with, right now it's very ambiguous.
As for making textboxes / dropdownlists "invisible" you can accomplish this through Javascript:
<input type=text id=text1>
<input type=button value=hide onclick=hide()>
<input type=button value=show onclick=show()>
<script type=text/javascript>
function hide(){
document.getElementById('text1').style.visibility='hidden';
}
function show(){
document.getElementById('text1').style.visibility='visible';
}
</script>
Web Pages are stateless. Microsoft brought in ViewState in ASP.NET 1.0 to accommodate for this. What I mean by stateless is there is no consistent connection between the client (users's computer) and the server (what hosts the web page the client is looking at). THe ViewState is a medium that was created to help bridge this buy retaining values on form controls during postbacks.
If you are using ASP.NET MVC, you will lose the data in your boxes. ASP.NET MVC does not use the ViewState (thankfully). This is why it's recommended that with ASP.NET MVC you use AJAX to communicate to your server. If you would like to understand more about this, check this out:
asp.net c# MVC: How do I live without ViewState?
If you are using regular website/web application projects, your data will remain intact if you enable the viewstate (This is enabled by default).
To disable / enable viewstate see the following for ASPX headers:
<%# Page Language="C#" EnableViewState="false" %>
<%# Page Language="C#" EnableViewState="true" %>
Viewstate:
http://msdn.microsoft.com/en-us/library/ms972976.aspx

Not able to get data in content page through javascript in asp.net

I have created a small web application for display the selected employees data. In this application I coded a javascript. I have create a dropdownbox with username details along with them a checkbox is in front of every user. These username get from database. I want that manager dropdown the list and select the employee which he want to see the details. I did this with the help of javascript but the issue is that it successfully display the usernames in dropdown when it is saparate form from the master page but when i merge it with master page means make it content page of that master page it doesn't display the usernames in dropdown.
It display it blank. Where I m doing wrong. Any suggestions will be appreciated.
Thanks in advance.
As Pleun says the masterpage will change the HTML control ids rendered to the browser. There are a couple of ways round this If you are using asp.net 4 you can specify the client ids as follows:
<asp:Label Text="text" runat="server" ID="SomeID" ClientIDMode="Static" />
That will maintain the ID rendered to the browser.
If you are user a previous version of asp.net then i tend to user JQuery to get the controls using something like:
$("[id$='SomeId']")
The $= means ends with.
You can also user server markup mixed in with your javascript code e.g.
var control = document.getElementById("<% =SomeId.ClientId %>");
asp.net will then render out whatever client side id it assigns to the control with server id SomeId
The master page changes the names of the controls:
Have a look here:
http://www.asp.net/master-pages/tutorials/control-id-naming-in-content-pages-cs
(Not your question - but the functionality can also be done out of the box with ASP.NET without coding javascript yourself)
You need to give the client id of the controls in the function.
When you were using master page den the id of ur controls changes.
For eg:
The clientID of Checkbox chktest will be chktest without master page.But with masterpage the clientID changes to something like 'ctl00_ContentPlaceHolder1_chktest'
So the javascript will not detect if you use chktest when using master page and your functions wont work as expected.So use clientId of controls.

Choose Header in ASP.NET Page

I have created a master page (Site.master) which contains the code to display a header, a footer, and a side bar. It works really great, but I am having trouble figuring out how to dynamically choose the header.
Basically, there are two possible header options. If the user is not logged in, I want them to see a login box and links for recovering their password, etc. If they are logged in, they'll see a logout link, and some information about their account (similar to how SO works, actually).
Is it possible to have the Site.master check and use whichever header I want depending on the login status of the user? I'm pretty stuck on where to begin with this (I thought maybe some checks in the code-behind of the master page) so any help would be appreciated.
You should consider using the built-in control, LoginView (MSDN). It is specifically designed to provide multiple templates (views), for authenticated and anonymous users.
This is a best practice approach. You can define your headers/footers etc. for logged in and anonymous users, with appropriate login/logout buttons, user information, etc. etc.
Here's a very basic example:
<asp:LoginView id="LoginView1" runat="server">
<AnonymousTemplate>
<asp:HyperLink ID="lnkLogin" runat="server" NavigateUrl="~/Login.aspx" Text="Login"/>
</AnonymousTemplate>
<LoggedInTemplate>
You are logged in as: <asp:LoginName id="lnCurrentUser" runat="server" />.
</LoggedInTemplate>
</asp:LoginView>
The .NET framework will handle the rest, and display the correct template without any extra code. If you end up using multiple roles in your application you can take this one step further and define templates for these roles as well (administrator vs regular user, etc.)
A perfect solution to your question based on the above: How to: Display Different Information to Anonymous and Logged In Users
Yes, very easily by putting the two possible headers in their own Panel controls and just saying the following in the Page_Load:
if ( Request.IsAuthenticated )
{
// Display
pnlAuthenticated.Visible = true;
pnlGuest.Visible = false;
}
else
{
// Display
pnlAuthenticated.Visible = false;
pnlGuest.Visible = true;
}
Personally I would put each set of header controls into 2 different placeholders and by default set both to invisible
Then with some code in Master Page
PlaceHolder1.Visible = Context.User.IsAuthenticated
PlaceHolder2.Visible = !Context.User.IsAuthenticated
Yes two ways of doing it; embed the header in a panel, and show/hide the panel depending on login status (which occurs in code). Alternatively, you could use two master pages, and do this check in the OnPreInit method (or PreInit event handler) and switch to show which master page you want to use (you can only change master pages programmatically in this event handler).
The problem with the second option is that HttpContext.Current.user may not be available in PreInit...
HTH.

Categories