private static void SetCurrentUserOwinEnvironmentKey(this AppController self, User user) { if (user != null) { self.OwinContext.Environment[Constants.CurrentUserOwinEnvironmentKey] = user; } else { self.OwinContext.Environment.Remove(Constants.CurrentUserOwinEnvironmentKey); } }
public static void SetCurrentUser(this AppController self, User user, Credential credential = null) { if (user == null) { // Reset the current user self.OwinContext.Request.User = null; self.OwinContext.Environment.Remove(Constants.CurrentUserOwinEnvironmentKey); return; } SetOwinContextCurrentUser(self, user, credential); self.OwinContext.Environment[Constants.CurrentUserOwinEnvironmentKey] = user; }
/// <summary> /// Should only be used in the rare cases where you are testing an action that /// does NOT use AppController.GetCurrentUser()! In those cases, use /// TestExtensionMethods.SetCurrentUser(AppController, User) instead. /// </summary> /// <param name="name"></param> public static void SetCurrentUser(this AppController self, string name) { var principal = new ClaimsPrincipal( new ClaimsIdentity( new [] { new Claim(ClaimTypes.Name, String.IsNullOrEmpty(name) ? "theUserName" : name) })); var mock = Mock.Get(self.HttpContext); mock.Setup(c => c.Request.IsAuthenticated).Returns(true); mock.Setup(c => c.User).Returns(principal); self.OwinContext.Request.User = principal; }
/// <summary> /// Should only be used in the rare cases where you are testing an action that /// does NOT use AppController.GetCurrentUser()! In those cases, use /// TestExtensionMethods.SetCurrentUser(AppController, User) instead. /// </summary> public static void SetOwinContextCurrentUser(this AppController self, User user, Credential credential = null) { if (user == null) { self.OwinContext.Request.User = null; return; } ClaimsIdentity identity = null; if (credential != null) { identity = AuthenticationService.CreateIdentity( user, AuthenticationTypes.ApiKey, new Claim(NuGetClaims.ApiKey, credential.Value), new Claim(NuGetClaims.Scope, JsonConvert.SerializeObject(credential.Scopes, Formatting.None))); } else { if (string.IsNullOrEmpty(user.Username)) { user.Username = "******"; } identity = AuthenticationService.CreateIdentity( user, AuthenticationTypes.External); } var principal = new ClaimsPrincipal(identity); var mock = Mock.Get(self.HttpContext); mock.Setup(c => c.Request.IsAuthenticated).Returns(true); mock.Setup(c => c.User).Returns(principal); self.OwinContext.Request.User = principal; }
public NuGetContext(AppController ctrl) { Config = Container.Kernel.TryGet <ConfigurationService>(); _currentUser = new Lazy <User>(() => ctrl.OwinContext.GetCurrentUser()); }
public static void SetCurrentUser(this AppController self, User user, Credential credential = null) { SetOwinContextCurrentUser(self, user, credential); self.OwinContext.Environment[Constants.CurrentUserOwinEnvironmentKey] = user; }
public static void SetCurrentUser(this AppController self, User user) { SetCurrentUser(self, user.Username); self.OwinContext.Environment[Constants.CurrentUserOwinEnvironmentKey] = user; }
public NuGetContext(AppController ctrl) { Config = DependencyResolver.Current.GetService <IGalleryConfigurationService>(); _currentUser = new Lazy <User>(() => ctrl.OwinContext.GetCurrentUser()); }
public static void SetCurrentUser(this AppController self, User user, Credential credential = null) { self.SetOwinContextCurrentUser(user, credential); self.SetCurrentUserOwinEnvironmentKey(user); }
public static void SetCurrentUser(this AppController self, User user, string scopes = null) { SetOwinContextCurrentUser(self, user, scopes); self.OwinContext.Environment[Constants.CurrentUserOwinEnvironmentKey] = user; }