猿问

登录后未存储 UserPrincipal.EmployeeId

我正在使用 ActiveDirectory 身份验证(来自 Microsoft.Owin),这非常适合我的需要,但我遇到了一个问题,即即使我在登录过程中设置了 EmployeeId 也没有存储它。我仍在尝试学习这个库,所以我有点困惑。这是我的代码:


广告认证服务.cs


public class AdAuthenticationService

{

    public class AuthenticationResult

    {

        public string ErrorMessage { get; private set; }


        public bool IsSuccess => string.IsNullOrEmpty(ErrorMessage);


        public AuthenticationResult(string errorMessage = null)

        {

            ErrorMessage = errorMessage;

        }

    }


    private readonly IAuthenticationManager authenticationManager;


    public AdAuthenticationService(IAuthenticationManager authenticationManager)

    {

        this.authenticationManager = authenticationManager;

    }


    public AuthenticationResult SignIn(String username, String password)

    {

        // authenticates against your domain account

        ContextType authenticationType = ContextType.Domain;

        PrincipalContext principalContext = new PrincipalContext(authenticationType);

        bool isAuthenticated = false;

        UserPrincipal userPrincipal = null;

        try

        {

            var eacId = DatabaseOperations.Login(username.Trim(), password.Trim());

            if (eacId > 0)

            {

                principalContext.ValidateCredentials(username.Trim(), password.Trim(), ContextOptions.Negotiate);

                isAuthenticated = DatabaseOperations.Security(eacId) > 0;

                if (isAuthenticated)

                {

                    userPrincipal = new UserPrincipal(principalContext,

                        DatabaseOperations.GetFullNameByEacId(eacId), password, true);

                    userPrincipal.EmployeeId = eacId.ToString();

                }

            }

        }

    }

}

在 SignIn 方法中,您可以在 下设置 eacId userPrincipal.EmployeeId,但是当我尝试从项目中的其他任何位置访问它时,它返回 null。


我假设由于 CreateIdentity 方法,我应该向身份对象添加一个新声明?但是该对象对于 EmployeeId 没有任何内容,这正是我所需要的。


幕布斯7119047
浏览 126回答 1
1回答
随时随地看视频慕课网APP
我要回答