Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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# 在my owin授权提供程序的GrantResourceOwnerCredentials中设置自定义主体后,HttpContext.Current.User.Identity.Name为null_C#_Asp.net_Asp.net Web Api_Oauth - Fatal编程技术网

C# 在my owin授权提供程序的GrantResourceOwnerCredentials中设置自定义主体后,HttpContext.Current.User.Identity.Name为null

C# 在my owin授权提供程序的GrantResourceOwnerCredentials中设置自定义主体后,HttpContext.Current.User.Identity.Name为null,c#,asp.net,asp.net-web-api,oauth,C#,Asp.net,Asp.net Web Api,Oauth,以下是我的GrantResourceOwnerCredentials方法: public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

以下是我的GrantResourceOwnerCredentials方法:

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

        AccountModels.UserProfile user = ApplicationUserManager.UserLogin(new AccountModels.LoginModel { UserName = context.UserName , Password = context.Password, RememberMe= false });

        if (user == null)
        {
            context.SetError("invalid_grant", "The user name or password is incorrect.");
            return;
        }
        else
        {
            if (user.IsLoggedIn = false)
            {
                context.SetError("invalid_grant", "The user is no longer active. Please contact support for account activation");
                return;
            }
            else
            {
                var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
                identity.AddClaim(new Claim(ClaimTypes.Role, user.UserRole));


                var roles = new string[] {user.UserRole};

                AuthenticationProperties properties = CreateProperties(user.UserName, roles, user.IsEmilConfirmed);
                AuthenticationTicket ticket = new AuthenticationTicket(identity, properties);

                context.Validated(ticket);

                MyCustomPrincipal newUser = new MyCustomPrincipal(user.UserName);
                newUser.Id = user.UserID;
                newUser.Email = user.UserName;
                newUser.Role = user.UserRole;

                SetPrincipal(newUser);
                context.Request.Context.Authentication.SignIn(identity);

            }
        }
    }
我必须实现一个自定义的登录逻辑来支持我的旧数据库模式,登录后我想在httpcontext.current.user中设置刚刚登录的用户

以下是我的主要方法:

private static void SetPrincipal(IPrincipal principal)
    {
        Thread.CurrentPrincipal = principal;
        if (HttpContext.Current != null)
        {
            HttpContext.Current.User = principal;
        }
    }

授权后,每当我在控制器中调用HttpContext.Current.User.Identity.Name时,我都会得到null值。我错过了什么?有人能告诉我一些想法吗

尝试在控制器中使用
用户
对象:

var principal = User as ClaimsPrincipal;

旁注:如果您使用OWIN主机,则在GrantResourceOwnerCredentials中不需要调用
SetPrincipal

请解释启动时不调用SetPrincipal是什么意思。Setprincipal是在GrantResourceOwnerCredentials方法中调用的。我的错误,很可能与您的问题无关,或者我的措辞也不正确。我改正了。