src/NuGetGallery.Services/Extensions/IOwinContextExtensions.cs
// This is a method because the first call will perform a database call
/// <summary>
/// Get the current user, from the database, or if someone in this request has already
/// retrieved it, from memory. This will NEVER return null. It will throw an exception
/// that will yield an HTTP 401 if it would return null. As a result, it should only
/// be called in actions with the Authorize attribute or a Request.IsAuthenticated check
/// </summary>
/// <returns>The current user</returns>
Get the current user, from the database, or if someone in this request has already
retrieved it, from memory. This will NEVER return null. It will throw an exception
that will yield an HTTP 401 if it would return null. As a result, it should only
be called in actions with the Authorize attribute or a Request.IsAuthenticated check
Get the current user from the database or if someone in this request has already retrieved it from memory . This will NEVER return null . It will throw an exception that will yield an HTTP 401 if it would return null . As a result it should only be called in actions with the Authorize attribute or a Request . IsAuthenticated check
public static User GetCurrentUser(this IOwinContext self)
        {
            if (self.Request.User == null ||
                (self.Request.User.Identity != null && !self.Request.User.Identity.IsAuthenticated))
            {
                return null;
            }
            User user = null;
            object obj;
            if (self.Environment.TryGetValue(CurrentUserOwinEnvironmentKey, out obj))
            {
                user = obj as User;
            }
            if (user == null)
            {
                user = LoadUser(self);
                self.Environment[CurrentUserOwinEnvironmentKey] = user;
            }
            if (user == null)
            {
                self.Authentication.SignOut();
                throw new CurrentUserDeletedException();
            }
            return user;
        }
public static User GetCurrentUser ( this IOwinContext self ) { if ( self . Request . User == null || ( self . Request . User . Identity != null && ! self . Request . User . Identity . IsAuthenticated ) ) { return null ; } User user = null ; object obj ; if ( self . Environment . TryGetValue ( CurrentUserOwinEnvironmentKey , out obj ) ) { user = obj as User ; } if ( user == null ) { user = LoadUser ( self ) ; self . Environment [ CurrentUserOwinEnvironmentKey ] = user ; } if ( user == null ) { self . Authentication . SignOut ( ) ; throw new CurrentUserDeletedException ( ) ; } return user ; }
Get the current user, from the database, or if someone in this request has already
retrieved it, from memory.
Get the current user from the database or if someone in this request has already retrieved it from memory .
// Unauthorized! If we get here it's because a valid session token was presented, but the // user doesn't exist any more. So we just have a generic error.
The current user
The current user
IOwinContext
this