Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用声明标识对系统中的用户进行身份验证_C#_Asp.net Mvc_Asp.net Mvc 5_Owin_Claims Based Identity - Fatal编程技术网

C# 使用声明标识对系统中的用户进行身份验证

C# 使用声明标识对系统中的用户进行身份验证,c#,asp.net-mvc,asp.net-mvc-5,owin,claims-based-identity,C#,Asp.net Mvc,Asp.net Mvc 5,Owin,Claims Based Identity,我需要登录一个用户(借助用户名)到系统通过索赔身份和以下是我试图实现的 借助用户名从数据库中获取用户详细信息并创建用户对象 并将对象传递给CreateIdentityAsync UserManager.CreateIdentityAsync(用户,DefaultAuthenticationTypes.ApplicationOkie) 目前,该应用程序已启用多租户。上述方法仅适用于租户Id为null的记录。但对于tenent id不为null的其他有效记录,它会抛出错误 找不到用户ID 从user

我需要登录一个用户(借助用户名)到系统通过索赔身份和以下是我试图实现的

  • 借助用户名从数据库中获取用户详细信息并创建用户对象

  • 并将对象传递给CreateIdentityAsync

  • UserManager.CreateIdentityAsync(用户,DefaultAuthenticationTypes.ApplicationOkie)

    目前,该应用程序已启用多租户。上述方法仅适用于租户Id为null的记录。但对于tenent id不为null的其他有效记录,它会抛出错误

    找不到用户ID

    从userManager.CreateIdentityAsync

    因此,我尝试创建一个自定义索赔标识并登录到系统,如下所示

            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    
            List<Claim> claims = new List<Claim>{
        new Claim(ClaimTypes.GivenName, newUser.Name), //user.Name from my database
        new Claim(ClaimTypes.NameIdentifier, newUser.Id.ToString()), //user.Id from my database
        new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "MyApplicationName"),      
        new Claim(ClaimTypes.Email, newUser.EmailAddress),
        new Claim(ClaimTypes.Surname, newUser.Surname)
    };
            ClaimsIdentity identity = new System.Security.Claims.ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.Name, ClaimTypes.Role);
    
            AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);
    
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    清单索赔=新清单{
    新声明(ClaimTypes.GivenName,newUser.Name),//我的数据库中的user.Name
    新声明(ClaimTypes.NameIdentifier,newUser.Id.ToString()),//我的数据库中的user.Id
    新索赔(”http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider“,“MyApplicationName”),
    新索赔(ClaimTypes.Email、newUser.EmailAddress),
    新索赔(索赔类型.姓氏,新用户.姓氏)
    };
    ClaimsIdentity identity=new System.Security.Claims.ClaimsIdentity(Claims,DefaultAuthenticationTypes.applicationcokie,ClaimTypes.Name,ClaimTypes.Role);
    SignIn(新的AuthenticationProperties(){IsPersistent=false},标识);
    
    由于某种原因,它也失败了

    谁能帮我解决这个问题。通过查看如何实现索赔标识,如何通过索赔标识将用户登录到系统。我能够成功创建自定义索赔标识,如下所示

        string IdentityProviderClaimType =
        "http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider";
        string DefaultIdentityProviderClaimValue = "ASP.NET Identity";
    
        var id = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
        id.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(), ClaimValueTypes.String));
        id.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, user.UserName, ClaimValueTypes.String));
        id.AddClaim(new Claim(IdentityProviderClaimType, DefaultIdentityProviderClaimValue, ClaimValueTypes.String));
       //Provide the below if you have any role name 
            id.AddClaim(new Claim(ClaimsIdentity.DefaultRoleClaimType,role.Name , ClaimValueTypes.String));
    
    希望这对别人有帮助