I am trying to unit test my code which includes the line:
UserLoginInfo userIdentity = UserManager.GetLogins(User.Identity.GetUserId()).FirstOrDefault(); I'm just stuck on one bit as I can't get:
User.Identity.GetUserId() to return a value. I have been trying the following in the set-up of my controller:
var mock = new Mock<ControllerContext>(); mock.Setup(p => p.HttpContext.User.Identity.GetUserId()).Returns("string"); But it gives an error of "NotSupportedException was unhandled by user code". I have also tried the following:
ControllerContext controllerContext = new ControllerContext(); string username = "username"; string userid = Guid.NewGuid().ToString("N"); //could be a constant List<Claim> claims = new List<Claim>{ new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", username), new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", userid) }; var genericIdentity = new GenericIdentity("Andrew"); genericIdentity.AddClaims(claims); var genericPrincipal = new GenericPrincipal(genericIdentity, new string[] { }); controllerContext.HttpContext.User = genericPrincipal; Based on some code I found on stackoverflow, but this returns the same error "NotSupportedException was unhandled by user code".
Any help as to how I proceed would be appreciated. Thanks.
IIdentitythat abstracts your code from theGetUserIdextension method and set it as a dependency on the code that is calling yourUserManager