简体   繁体   English

如何将值从一个 controller 传递到 .netcore 中的另一个 controller

[英]how to pass value from one controller to another controller in .netcore

Logged User Id stored into another table.记录的用户 ID 存储到另一个表中。 How to Pass the Login UserId to the another controller.如何将登录用户 ID 传递给另一个 controller。 ** public async Task Login([FromBody] LoginModel model) { ** 公共异步任务登录([FromBody] LoginModel 模型){

        var user = await userManager.FindByNameAsync(model.Username);
        if (user != null && await userManager.CheckPasswordAsync(user, model.Password))
        {
            var userRoles = await userManager.GetRolesAsync(user);

            var authClaims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
            };

            foreach (var userRole in userRoles)
            {
                authClaims.Add(new Claim(ClaimTypes.Role, userRole));
            }

            var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:Secret"]));

            var token = new JwtSecurityToken(
                issuer: _configuration["JWT:ValidIssuer"],
                audience: _configuration["JWT:ValidAudience"],
                expires: DateTime.Now.AddDays(3),
                claims: authClaims,
                signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256)
                );

            return Ok(new
            {
                token = new JwtSecurityTokenHandler().WriteToken(token),
                username=user.UserName,
                expiration = token.ValidTo
            });
        }
        return Unauthorized();
    }
   Login **

You can use Temp Data:您可以使用临时数据:

 TempData["UserID"] = user.ID;

In a different controller:在不同的 controller 中:

 var userID = TempData["UserID"];

Using session:使用 session:

Configure session state in startup class:在启动 class 中配置 session state:

In ConfigureServices:在配置服务中:

services.AddDistributedMemoryCache();
services.AddSession(options =>
{
    options.IdleTimeout = TimeSpan.FromMinutes(10);
    options.Cookie.HttpOnly = true;
    options.Cookie.IsEssential = true;
});

In confugure:在配置中:

 app.UseSession();
  1. Define a string定义一个字符串

    public const string SessionUserId = string.Empty();
  2. We set a string我们设置一个字符串

    if(string.IsNullOrEmpty(HttpContext.Session.GetString(SessionUserId))) HttpContext.Session.SetString(SessionUserId, user.ID.ToString());
  3. We can get defined string:我们可以得到定义的字符串:

     var name = HttpContext.Session.GetString(SessionUserId);

More info in documentation: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-5.0文档中的更多信息: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-5.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将值从一个Tabbar控制器传递到Xamarin中的另一个控制器 - Pass value from one Tabbar controller to another controller in Xamarin 如何将对象列表从一个控制器传递到另一控制器? - how to pass list of objects from one controller to another controller? 如何将值从控制器方法传递给另一个方法? - How to pass a value from a controller method to an another? 如何将 Model 从 controller 传递到另一个 controller - how to pass a Model from a controller to another controller 如何在MVC中将XDocument从一个控制器传递到另一个 - How to pass XDocument from one controller to another in mvc 如何将大字符串从一个控制器传递到另一个控制器? - How can I pass large strings from one controller to another? 如何将AntiForgeryToken从一个控制器动作传递给另一个控制器动作? - How to pass an AntiForgeryToken from one controller action to another? 如何将模型数据传递给一个控制器到另一个控制器 - How to pass model data to one controller to another controller 如何将值从一个控制器传递到ASP.Net MVC3中的另一个控制器 - How to pass values from One controller to another Controller in ASP.Net MVC3 在将一个控制器传递给另一控制器时遇到问题 - Getting issue to pass one controller to another controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM