FormsAuthentication with my own DB - get logged on user
Hi,
First of all, I'm very new to ASP.NET (though not .NET in general), so my apologies if something is obvious... It's not for me :)
I am trying to build a simple login system in my website. I did some searching and found that there are quite a few 'templates' (such as the Login controls) that Visual Studio provides for you. However, by default those need a MS SQL Server database. This is a problem for me because
1. I don't have a MS SQL database,
2. My webspace provider does support it, but only for an additional fee, and I feel paying an additional fee just for a simple login system is... no.:wave:
3. I'd like to keep it all in my existing (Access) database.
I came across this website which shows me how to handle the login system with an Access database.
As far as I can tell all I need is the username and then I can get all the built-in login controls and such working.
So, I implemented this, and it seems to work fine. I can login using the Login control, I can display or hide content whether or not the user is logged in using the LoginView control, I can display a Login / Logout link, etc.
The only thing I cannot seem to do is get the name of the currently logged in user (if any). All I need is the name, I can get the rest via the database manually that's no problem.
There is the LoginName control which displays that name to me, but I cannot figure out how to retrieve it in code.
I did found the Membership.GetUser() method, but it doesn't seem to work (because I'm using my own Access database? I dunno), it returns Nothing even if a user is logged in (the LoginName control shows the name).
How can I do this..? Thanks!
Re: FormsAuthentication with my own DB - get logged on user
Hey Nick,
Try this:
Code:
System.Web.HttpContext.Current.User.Identity.Name
More info here:
http://msdn.microsoft.com/en-us/libr...tity.name.aspx
and here:
http://msdn.microsoft.com/en-us/libr...text.user.aspx
Hope that helps!
Gary
Re: FormsAuthentication with my own DB - get logged on user
Awesome, it worked just fine :) I'd expected it to be harder. I must say I can't really get my head around the user/login/security stuff at the moment (maybe cause it's getting late..). It seems there are like 20 different ways to authenticate users and you can even mix all of them up :/ Maybe it's just me lol but I can't make much sense of it yet :p
Thanks!