English 中文(简体)
ASP.NET 核心用途(Json Web Token)
原标题:ASP.NET Core use (Json Web Token) JWT to authenticate a session

i 试图在我的微笑网页应用上添加对《维也纳条约法公约》的认证。 我希望用户进入电子邮件地址,如果电子邮件在我的数据库中存在的话,则不进行检查。 如果该地址储存在数据库中,则希望在一届会议上打字并储存。

i 没有发现任何关于如何用平流/管/内联网执行类似的东西。 因此,你们有某种想法,可以如何用raz页确定这一点?

我试图追随一些教导,但没有任何事实。

页: 1 我的方案。

services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(o =>
        {
            o.RequireHttpsMetadata = false;
            o.SaveToken = true;
            o.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JwtSettings:Key"])),
                ValidateIssuer = true,
                ValidIssuer = configuration["JwtSettings:Issuer"],
                ValidateAudience = false,
                ValidateLifetime = true,
            };
        });

        services.AddAuthorization();

2. 我的后遗忘了原木,紧随此而去。 在座标中,我想在一届会议上或座标上储存脚印。

        public async Task<IActionResult> OnPost()
        {   
            await _loginRepository.CreateToken(Email);
//TODO store token here
            return RedirectToPage("/Picture/Index");
        }

在对电子邮件是否存在进行核对后,即可产生一个信号,然后改用<代码>保护的网页[Autroize(Roles = “Admin”)]。

  1. 我的象征性一代

    public async Task<bool> CreateToken(string email, CancellationToken cancellationToken = default)
     {
         var journalists = await _zentralContext.v_PressInfo_Journalisten.Select(s => s.Email).ToListAsync();
         if (journalists.Contains(email))
         {    
             var journalist = await _zentralContext.v_PressInfo_Journalisten.Where(w => w.Email == email).FirstAsync();
             var token = CreateToken(email);
             //Store Token here??
             return true;
         }
         else
         {
             return false;
         }
     }
    
    
     private string CreateToken(string email)
     {
         var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("TheSecretKeyNeedsToBePrettyLongSoWeNeedToAddSomeCharsHere"));
         var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
    
         var claims = new[]
         {
             new Claim(ClaimTypes.Email, email),
             new Claim(ClaimTypes.Role, "Admin")
         };
         var token = new JwtSecurityToken(
             issuer: "Presseinfo Extern",
             claims: claims,
             expires: DateTime.Now.AddDays(1),
             signingCredentials: creds);
         return new JwtSecurityTokenHandler().WriteToken(token);
     }
    
问题回答

问题 您似乎想把所产生结果储存在罗辛存放处。 因此,你可以提及这种简单的说法。

注册号:HttpContextAccessor,载于您的扶养注射集装箱内:

builder.Services.AddHttpContextAccessor();

将这一点输入贵处:

public class LoginRepository : IloginRepository
    {
        private readonly MyDbContext _myDbContext;
        private readonly IHttpContextAccessor _contextAccessor;

        public LoginRepository(MyDbContext myDbContext, IHttpContextAccessor contextAccessor)
        {
            _myDbContext = myDbContext;
            _contextAccessor = contextAccessor;
        }

        public async Task<bool> CreateToken(string email, CancellationToken cancellationToken)
        {
            var result =await _myDbContext.loginModels.FirstOrDefaultAsync(x => x.Email == email);
            if (result != null)
            {
                var token = CreateToken(email);
                _contextAccessor.HttpContext.Session.SetString("token", token);
                return true;
            }
            return false;
        }

        private string CreateToken(string email)
        {
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("TheSecretKeyNeedsToBePrettyLongSoWeNeedToAddSomeCharsHere"));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var claims = new[]
            {
                new Claim(ClaimTypes.Email, email),
                new Claim(ClaimTypes.Role, "Admin")
            };
            var token = new JwtSecurityToken(
                issuer: "Presseinfo Extern",
                claims: claims,
                expires: DateTime.Now.AddDays(1),
                signingCredentials: creds);
            return new JwtSecurityTokenHandler().WriteToken(token);
        }
    }

现在 如果电子邮件地址储存在数据库中,贵项目将产生信号并储存到会场。

Ps: 不要忘记在你的项目中举行会议,你可以提到:





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....