Search code examples
c#sql-serverasp.net-mvcauthenticationasp.net-identity

Using aspnet Identity with existing MS SQL Server, UserId not found error


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);
    }

Solution

  • 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.