csharp 向会话添加新属性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 向会话添加新属性相关的知识,希望对你有一定的参考价值。

//Add new property to claims on login

private async Task SignInAsync(User user, ClaimsIdentity identity = null, bool rememberMe = false)
{
    if (identity == null)
    {
        identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    }

    identity.AddClaim(new Claim("Application_UserEmail", user.EmailAddress)); //SETTING NEW PROPERTY

    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe }, identity);
}
//Create a custom session class 

public class MyAppSession : ITransientDependency
{
    public string UserEmail
    {
        get
        {
            var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
            if (claimsPrincipal == null)
            {
                return null;
            }

            var emailClaim = claimsPrincipal.Claims.FirstOrDefault(c => c.Type == "Application_UserEmail");
            if (emailClaim == null || string.IsNullOrEmpty(emailClaim.Value))
            {
                return null;
            }

            return emailClaim.Value;
        }
    }
}
//Getting session property using MyAppSession

[AbpAuthorize]
public class SessionAppService
{
    private readonly MyAppSession _mySession;

    public SessionAppService(MyAppSession mySession)
    {
        _mySession = mySession;
    }

    public void Test()
    {
        var userEmailFromSession = _mySession.UserEmail;
    }
}

以上是关于csharp 向会话添加新属性的主要内容,如果未能解决你的问题,请参考以下文章

csharp Umbraco安装包动作,向仪表板添加新的“选项卡”(不是“部分”)。用于:https://github.com/EndzoneSoftware/uWhiteL

sh [记录SSH会话(高级)]尝试向脚本添加一些项目,因此创建一个新条目#bash

csharp MVC 4会话属性

使用 NodeJS 向 JSON 文件键添加新属性

无法向 Mongoose 查询返回的对象添加新属性 [重复]

无法向 Mongoose 查询返回的对象添加新属性 [重复]