Show logged in user in HTML page secured with IIS Forms Authentication using jQuery or similar?
I have a website with a lot of static HTML pages. There are also some aspx pages in the site. We are using Forms Authentication with standard login controls and the default ASP.NET authentication provider to control user access. Only the aspx pages have their access or functionality controlled by who is logged in. The HTML pages are all open to the public.
However, we'd like to be able to show users who are logged in that they are logged in, even on the static HTML pages. We've got a link in the 开发者_运维问答top right corner of the HTML pages to take the user to the login.aspx page. Is there any way to change this link, using jQuery or something similar so that users who are authenticated see their login name or even a link that logs them out? Ideally we'd like to show them their login name as a link that logs them out.
If this is possible, does anyone have an example of the code to do this that they can share?
It's definitely possible, here's an overview of the solution I'd use:
I'd use the jquery.ajax function to hit an asp.net generic handler (.ashx) page. There are plenty example of this on the web but as a quick example:
$.ajax({
type: 'POST',
url: myServerSidePage.ashx,
success: function (data) {
//update link here
}
});
Then on the myServerSidePage.ashx I'd have a method along the lines of:
public void ProcessRequest(HttpContext context)
{
context.Response.Write(Membership.GetUser().UserName);
}
You'd obviously need some error checking around both the server side and ajax call as well.
精彩评论