How to retreive userID

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
8/29/2006 4:18:31 AM
Gravatar
Total Posts 19

How to retreive userID

Hi,

I'm developing a project tracking module.

I'd need to get the UserID of the table mp_Users when a user is loged in.

Is the UserID(which is an integer) already included in the class SiteUser.cs?

Can we obtain UserID from the cache(when user is loged in)?

Thanks

Thanh

8/29/2006 4:40:14 AM
Gravatar
Total Posts 18439

Re: How to retreive userID

Hi Thanh,

You are working with the 1.x version of mojoPortal in VS 2003 right?

I'm afraid there is nothing implemented there in terms of caching the siteUser.

In the 2.1.1 version there is a method SiteUser.GetCurrent() that checks the httpcontext for the siteUser and if its there it returns it otherwise it instantiates it, puts in  the http context then returns it. This ensures the user is only looked up 1 time during the request.

You can add this method to the SiteUser class:

public static SiteUser GetCurrent()
{
    if (HttpContext.Current != null)
    {
    if (HttpContext.Current.Items["CurrentUser"] != null)
    {
        return (SiteUser)HttpContext.Current.Items["CurrentUser"];
    }
    else
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
        try
        {
            SiteSettings siteSettings = SiteSettings.GetCurrent();
            SiteUser siteUser = new SiteUser(siteSettings, HttpContext.Current.User.Identity.Name);
            if (siteUser.UserID > 0)
            {
            HttpContext.Current.Items["CurrentUser"] = siteUser;
            return siteUser;
            }
        }
        catch { }
        }

    }
    }

    return null;
           

}

I will add it to the 1.x sources to keep it in sync for your current work though I may be changing this implementation soon in the 2.x branch

Hope it helps,

Joe
6/26/2007 6:15:03 PM
Gravatar
Total Posts 112

Re: How to retreive userID

Hi Joe,

How can I find the UserId in a module I've written for a recent SVN? I've based my module on your EmptyModule and have tried the revisions to SiteUser.cs mentioned above; but I get compile errors.

Perhaps you have taken a different path since you wrote this note,
Dale E. Moore

 

6/26/2007 8:08:57 PM
Gravatar
Total Posts 18439

Re: How to retreive userID

Hi Dale,

using mojoPortal.Web;

using mojoPortal.Business;

SiteUser currentUser = SiteUtils.GetCurrentUser();

now you have an instance of the user and can get the integer id with .UserID or the Guid id with .UserGuid

Hope it helps,

Joe

6/26/2007 8:39:30 PM
Gravatar
Total Posts 112

Re: How to retreive userID

Hi Joe,

GetCurrentUser() didn't exist. So I updated the SVN and now I get "Microsoft Visual Studio Unable to read the project file 'mojoPortal.Web.csproj'. mojoPortal\Web\mojoPortal.Web.csproj(7088,2); The project file could not be loaded. Name cannot begin with the '<' character, hexadecimal value 0x3C. Line 7088, position 2. OK Cancel". I found this at 7087-7090:

<Folder Include="Data\Sites\1\flash\" />
<<<<<<< .mine
=======
<Folder Include="Data\Sites\1\GalleryImages\EditorUploadImages\"

So I pulled 7088-9 and (wait for it)...

Unable to read 7092,45; The element <#text> beneath element <ItemGroup> is unrecognized.

7092-4:
<Folder Include="Data\Sites\1\index\" />
>>>>>>> .r2363
<Folder Include="Data\Sites\1\SharedFiles\History\" />

And it builds, but I still get:

Error 3 'mojoPortal.Web.SiteUtils' does not contain a definition for 'GetCurrentUser' D:\webs\MW\src\mojoPortal\Web\Modules\TechModule.ascx.cs 85 46 mojoPortal.Web

But I did find GetCurrentSiteUser
SiteUser currentUser = SiteUtils.GetCurrentSiteUser();

Now I get "A project with an Output Type of Class Library cannot be started directly" when I build the solution. But when I set mojoPortal.Web as the startup project, things go swimingly.

Errr the site debugs but the user is not "DaleEMoore" whom logged on, but instead "mojoPortal.Business.SiteUser".

Still scratching my head,
Dale E. Moore

6/26/2007 9:18:24 PM
Gravatar
Total Posts 18439

Re: How to retreive userID

My bad, I was typing from memory the method is SiteUtils.GetCurrentSiteUser(); as you found it.

if you want the name of the user its .Name, Email is .Email and so on for any user property.

Hope it helps,

Joe

6/26/2007 9:19:31 PM
Gravatar
Total Posts 18439

Re: How to retreive userID

or if all you need is the name then you can also use HttpContext.Current.User.Identity.Name

Joe

6/26/2007 11:18:25 PM
Gravatar
Total Posts 112

Re: How to retreive userID

This works PERFECTLY, thanks!

8/18/2007 12:23:24 AM
Gravatar
Total Posts 4
www.seo4company.com - China SEO

Re: How to retreive userID

How didi you lose your ID?

8/18/2007 12:23:41 AM
Gravatar
Total Posts 4
www.seo4company.com - China SEO

Re: How to retreive userID

How didi you lose your ID?

8/18/2007 12:23:59 AM
Gravatar
Total Posts 4
www.seo4company.com - China SEO

Re: How to retreive userID

How didi you lose your ID?

You must sign in to post in the forums. This thread is closed to new posts.