0

Hi I'm new to MVC having done web forms for several years.

When users reach our site they are already authenticated by our organization's central authentication service. We already have all users in our SQL Server database and just need to match their identity and assign the appropriate authorizations. I customized https://aspnet.codeplex.com/SourceControl/latest#Samples/Identity/AspNet.Identity.MySQL/Readme.txt for use with our existing SQL Server. I'm getting a "UserId not found" error. Here is the stack trace:

[InvalidOperationException: UserId not found.]
   Microsoft.AspNet.Identity.<GetSecurityStampAsync>d__42.MoveNext() +651
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +49
   Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +109
   Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +1350
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +49
   Security.Models.<GenerateUserIdentityAsync>d__0.MoveNext() in T:\Security\Models\IdentityModels.cs:16
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
   Microsoft.AspNet.Identity.Owin.<SignInAsync>d__2.MoveNext() +437
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
   Security.Controllers.<Login>d__10.MoveNext() in T:\Security\Controllers\AccountController.cs:77

Here is where the error occurs in IdentityModel.cs:

 public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

Below is my AccountController.cs code based on: ASP.NET MVC Identity login without password

    var user = await UserManager.FindByNameAsync(id);

    var result = await UserManager.CreateAsync(user);
    if (result.Succeeded)
    {
        await SignInManager.SignInAsync(user, isPersistent: true, rememberBrowser: true);
    }

3 Answers 3

1

I removed

var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

and replaced it with

    var userIdentity = new ClaimsIdentity(new[]
        {
                // adding following 2 claims for supporting default antiforgery provider
                new Claim(ClaimTypes.NameIdentifier, this.UserName),
                new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),
                new Claim(ClaimTypes.Name, this.UserName),
                new Claim("AspNet.Identity.SecurityStamp", this.SecurityStamp),
                //new Claim(ClaimTypes.Role, AppRoleClaims.Client),
                new Claim(ClaimTypes.Sid, this.Id.ToString())
        },
    DefaultAuthenticationTypes.ApplicationCookie);

I found this code at How can I log a Client in Asp.Net MVC with no user. It appears to have solved the issue I was having.

Sign up to request clarification or add additional context in comments.

Comments

0

If you have the userid change await UserManager.FindByNameAsync(id) -> await UserManager.FindByIdAsync(Id)

FindByNameAsync -> is finding for the matching username

1 Comment

Using FindByIdAsync does not solve the problem. I'm still getting the same error, UserId not found. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: UserId not found.
0

Hi you can use this method UserManager.FindByIdAsync Method, it will Finds a user by ID.

ex:

 var user = await UserManager.FindByIdAsync(userid);

3 Comments

Using FindByIdAsync does not solve the problem. I'm still getting the same error, UserId not found. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: UserId not found.
@Robert is value of UserId null by any chance?
I can't find where the UserId is. This article seems to be related however it doesn't appear to apply to me. stackoverflow.com/questions/34946575/… I don't have UserId in my IdentityUser class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.