示例#1
0
        /// <summary>
        ///     Extratcs the name of the user from the request
        /// </summary>
        /// <remarks>
        ///     There is a bug in DNOA where the <see cref="IAccessTokenRequest.UserName" /> is null when requesting a
        ///     refreshtoken.
        ///     Thsi method works around it by finding the username in the request.
        /// </remarks>
        private static string GetUserFromAccessTokenRequest(IAccessTokenRequest accessTokenRequest)
        {
            if (accessTokenRequest.UserName.HasValue())
            {
                return(accessTokenRequest.UserName);
            }

            var request = accessTokenRequest as IAuthorizationDescription;

            if (request != null)
            {
                return(request.User);
            }

            if (accessTokenRequest is AccessTokenRequestBase)
            {
                // Use reflection to get username
                Type type = accessTokenRequest.GetType();
                return((from p in type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance)
                        where typeof(IAuthorizationDescription).IsAssignableFrom(p.PropertyType)
                        select((IAuthorizationDescription)p.GetValue(accessTokenRequest)).User).FirstOrDefault());
            }

            return(null);
        }