Accessing User.ProviderUserKey through Page.User

I'm currently using the standard SqlMembershipProvider that ships with ASP.NET.  I have used the aspnet_regsql tool to add the membership tables to my database.  I would like to access User.ProviderUserKey on my pages and I'm wondering what the best way of doing this is.

Let me explain... Most tables in my database have a UserId (uniqueidentifier) column that stores the UserId of the user who contributed each row into the table.  In my pages that provide insert functionality I would like to just call the Insert method passing a reference to either my user or just the user's guid.  However, I can't seem to figure out how.  The best I've come up with in a one line solution is this: 

   Membership.GetUser(Page.User.Identity.Name).ProviderUserKey 

but that means an extra database call for each Insert.  There must be a better way of doing this, right?


Please click Mark as Answer if my reply helped you.

http://noahblu.wordpress.com

"Any fool can recall the flight of birds across a moonlit sky but it takes a genius to forget the face of his mother."
0
noahb
12/13/2007 6:36:57 PM
asp.net.security 27051 articles. 1 followers. Follow

2 Replies
656 Views

Similar Articles

[PageSpeed] 7

 As far as I know that is the only way. If you really want to avoid the second database call you could have your sql obtain the userid from the username and just have the username on the web side of your app. Then you can always in your sql know you have to take username and turn it into the userid.

I hope that helps,
Brendan 


C. Brendan Enrick
Brendan's Blog

Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
0
benrick
12/13/2007 7:10:26 PM

Thanks, yea, that does help actually - puts me in the right direction.

I figure I'll just extend the Page object and keep a (lazy) property called CurrentUserId.  That way it's easy to get when I need it, will only do the query once no matter how many inserts I need, and will exist for all pages.

 


Please click Mark as Answer if my reply helped you.

http://noahblu.wordpress.com

"Any fool can recall the flight of birds across a moonlit sky but it takes a genius to forget the face of his mother."
0
noahb
12/13/2007 8:48:36 PM
Reply:

Similar Artilces:

How to access one user control from another user control on same page
hi,I have two usercontrol on same page. 1.ascx and 2.ascx on abc.aspx . Now I need to access dropdown on 1.ascx frm 2.ascx . Plz help me how can i do this?I have tried Page.findcontrol but as a user control inherits usercontrol class and not page class . its not giving reference to proeprty Page.Findcontrol. I can do page.findcontrol on aspx page but I need to do it on ascx only.plz helpregards,max   Hi Max, you have to expose your DropDownList as a public property which lays inside usercontrol 1. Example: 1 class MyUserControl1 : UserControl 2 { 3 4 p...

Relogin using different user , can access the previous user login page
Hi  there , I am using ASP Net 2.0 and its login controls. I face security loophole in which within the same IE Window, the 1st user access a page and left for long time. And the 2nd user click on it and get redirect to login page. The second user login and can see the last page that the 1st user left. Why ? Any workaround for this ? Thanks  Hi wkhwan, When the user logs in the second time, they will by default be send back to the page that they were previously on.  Note that they will be accessing the page as the second user ...

Why must I give Authenticated Users rights to an aspx page to enable users to access it?
I am trying set up security on my application before deployment and I am having issues controlling acces to some pages. I'm using  Windows authentication and Active Directory roles and I have security trimming enabled. I have a folder called Quality Control and there are 4 aspx pages within. I have given Authenticated Users the rights they need to see and open 3 of the 4 pages in the Quality Control folder. I remove Authenticated Users from one page (QcOnly.aspx) that I only want members of the Quality Control role to have access to and I gave the ...

Authenticated users not getting access to secured pages????? What can I do?
Ok I have a simple login page using the login control. Once the user's name and password is found it should take them to main.aspx page. Only authorized users can access teh main.aspx page. Heres teh code for that:Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate        Dim Authenticated As Boolean = False        Authenticated = Authenticate(Login1.UserName, hashPwrd())        e.Authen...

bug ?? Relogin with different user, can see the last access page of the previous user logout using LoginStatus ??
Hi, I still dont understand why when user click Logout on the LoginStatus link, it does not clean up his last page access. When  another user ( with different role too) login using the same IE window, he get redirect to the previous user's last page access. Surprise to me and it looks like a security flaw. And thus i have to manually put codes to run formauthentication.signout as what Asp.net 1.1 usually do !!   Why loginstatus does not clean up the last user session ?? And furthermore, is there any method in loginstatu...

How to automatically send denied users to "access denied" page, instead of a login prompt only then to be sent to "access denied" page
Hello: My security works fine except for one small annoyance.  This is an intranet site only and are using Windows (AD) authentication with Anonymous turned off.  The 'good' users get right in, with no prompts.  This is good.  However, the 'bad' (denied) users get a prompt to enter their credentials only to be denied.  I would like to eliminate the need for them to even try and enter credentials and send them directly to an "access denied" page (no prompts).  It only wastes their time and aggrevates them. Thoughts? Tha...

Accessing Profile of users other than current user.
I want to access profile of other users. I want to allow my members to visit other members profile. I am not going to disply the senstive information. The default sql profile provider access to profile of current user only. Hi you need to use the getProfile function as follows:ProfileCommon UserProfile = Profile.GetProfile(UserName); UserProfile.PropertyName1=....   Please read this related thread : http://forums.asp.net/p/1243638/2279740.aspx   Regards,Regards,Anas Ghanem.Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wron...

Users customise their profile page for all users
Hi there I am a bit of a newbie to ASP.NET.  All my web development experience before has been with good old asp pages! So anyways here's what I was wondering.  I have a page called ViewProfile which displays the information about a particular user.  The information is pulled from a users table stored in SQL server.  The user profile displayed is determined by the id parameter passed in the url (for example - ViewProfile.aspx?id=123).  This allows other users of the system to see another users profile (the appropriate links etc are on the pages). Now here'...

customvalidator on a user control UC1 when the user clicks a button located on a different user control UC2 on the page
Hi, I am maintaining page where a user control is initialized  dynamically. The user control UC1 has a customvalidator using server side validation. When the postback is caused by a button inside the user control UC1 causes the postback, the customvalidator works fine and I get my validation results displayed. However, If I click a button located on a second user control UC2 on the main aspx page, the validation doesn t execute and I get no result validation error displayed. My question,how can I make the validation execute on the user control  UC1 when the postback is caused by...

Updatepanel
Hi, I have few tabs in my page, when user changes the tab, I am loading page contents Async'ly. I want to make all tabs are inaccessible to user during load time. How can I ? Thank you.  Regards, Sreedhar You can tap into the events of the PageRequestManager. It fires init, begin, and end request events when an async update occurs.  Sys.Application.add_init(Page_Init); var prm = Sys.WebForms.PageRequestManager.getInstance(); function Page_Init(sender) { prm.add_initializeRequest(Page_InitRequest); prm.add_beginRequest(Page_BeginRequest); prm.add_endRequest(...

Accessing user controls from pages within Master pages
Hi,  I have a problem with a web-site I have built which users Master Pages.  One of the pages has a userControl within it, and in the codebehind on the page being loaded into the master page, I access methods and properties on this control.  All builds fine. When I run it, the control does not load, and looking at the source code, the reason is that the control is prefixed with the name of the container of the master page, "ctl00_contentPage" which stops this control loading properly.  As soon as I amend the HTML generated and delete this prefix from the cont...

User access to a page
Hi, Can anyone point me in the right direction when it comes to allowing users to access certain pages only if they are privileged to do so. I am still in ASP mode and am not sure how to implement the whole privileged system for users to access their own account etc. Any documentation I should go straight to? What does the architecture look like? Is it simply the same as a regular ASP page where a session variable is checked for the right privilege level? Thanks for your help! I suggest you take a look at Rolebased Authorization which is far more advanced (but still easy) than ...

Unable to access user control's user control's function\property from another user control
Hi, I used to call an user control's user control function as stated below from my user control in ASP.Net 1.1 wucCompany.wucEmployee.GetEmployeeSomething() After migrating to ASP.Net 2.0, I am unable to use any properties/functions(even the public ones) of the user control's User control's from another user control.  The way I have to do is create property\function in wucCompany which calls the wucEmployee's property\function and call the wucDepartment.GetEmployeeSomething().Since I need to do this change in too many places, I can...

Denying access to a page to certain users redirects to login page
I have an administration page in my web application (which uses Forms Authentication) and I've protected it by making an authorization rule in the web.config for that particular page.  I've allowed one user access and denied all others by using the following:   </system.web> <location path="UserAdmin.aspx"> <system.web> <authorization> <allow users="AdminGuy" /> <deny users="*" /> </authorization> </system.web> </location> </configuration>  ...

Web resources about - Accessing User.ProviderUserKey through Page.User - asp.net.security

Resources last updated: 12/31/2015 8:53:52 AM