简体   繁体   English

Asp.Net Core 3.0 授权和认证

[英]Asp.Net Core 3.0 Authorization and Authentication

Wherever I look on the internet, identity is used in the login process related to net.core.无论我在 Internet 上的何处查看,都在与 net.core 相关的登录过程中使用身份。 Nobody talks about logging in with our normal username and password.没有人谈论使用我们普通的用户名和密码登录。 We are logging in but this time on checks We cannot use [Authorize(Roles="Admin")] or [Authorize] attribute.我们正在登录,但这次检查我们不能使用 [Authorize(Roles="Admin")] 或 [Authorize] 属性。 To use it, we need to login as follows.要使用它,我们需要按如下方式登录。

signInManager.PasswordSignInAsync (model.email, model.password, true, true); signInManager.PasswordSignInAsync (model.email, model.password, true, true);

Look at this link but the result is the same https://docs.microsoft.com/tr-tr/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1#ord is看看这个链接,但结果是一样的https://docs.microsoft.com/tr-tr/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1#ord

If it turns out to be a result here, we are trying for it.如果结果是这里的结果,我们正在努力。 What do I need to do to use the above attributes for my own login without using Policy, signInManager.PasswordSignInAsync is doing this exactly what I have added to the message as below, but it did not happen anyway.我需要做什么才能在不使用 Policy 的情况下将上述属性用于我自己的登录,signInManager.PasswordSignInAsync 正在执行我添加到消息中的操作,如下所示,但无论如何都没有发生。

My Login Code https://rextester.com/YBJ16358我的登录代码https://rextester.com/YBJ16358

My Startup我的创业

https://rextester.com/VZODZ96615 https://rextester.com/VZODZ96615

I solved the problem as follows.我解决了以下问题。 if username and password true如果用户名和密码为真

var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);

            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.UserName));
            identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
            identity.AddClaim(new Claim(ClaimTypes.GivenName, user.Name));
            identity.AddClaim(new Claim(ClaimTypes.Surname, user.Surname));
            identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));
            foreach (var role in _userManager.GetRolesAsync(user).Result)
            {
                identity.AddClaim(new Claim(ClaimTypes.Role, role));
            }
            ClaimsPrincipal principal = new ClaimsPrincipal(identity);
            AuthenticationProperties _authentication = new AuthenticationProperties
            {
                IsPersistent = true,
                ExpiresUtc = DateTimeOffset.UtcNow
            };
            await _HttpContextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = true });

My Startup我的创业

 services.AddAuthentication(options =>
            {
                options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie(config =>
            {
                config.Cookie.Name = "login";
                config.LoginPath = "/Account/Login";
                config.ExpireTimeSpan = TimeSpan.FromMinutes(5);
            });

and App和应用程序

   app.UseAuthentication();
   app.UseAuthorization();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM