• <tfoot id='N8j11'></tfoot>
    1. <i id='N8j11'><tr id='N8j11'><dt id='N8j11'><q id='N8j11'><span id='N8j11'><b id='N8j11'><form id='N8j11'><ins id='N8j11'></ins><ul id='N8j11'></ul><sub id='N8j11'></sub></form><legend id='N8j11'></legend><bdo id='N8j11'><pre id='N8j11'><center id='N8j11'></center></pre></bdo></b><th id='N8j11'></th></span></q></dt></tr></i><div id='N8j11'><tfoot id='N8j11'></tfoot><dl id='N8j11'><fieldset id='N8j11'></fieldset></dl></div>
        <bdo id='N8j11'></bdo><ul id='N8j11'></ul>

      1. <small id='N8j11'></small><noframes id='N8j11'>

      2. <legend id='N8j11'><style id='N8j11'><dir id='N8j11'><q id='N8j11'></q></dir></style></legend>

      3. 用于 Mongodb 数据存储的 asp.net 核心中基于简单令牌的身份验证/授权

        时间:2023-06-08

            <i id='0BT7T'><tr id='0BT7T'><dt id='0BT7T'><q id='0BT7T'><span id='0BT7T'><b id='0BT7T'><form id='0BT7T'><ins id='0BT7T'></ins><ul id='0BT7T'></ul><sub id='0BT7T'></sub></form><legend id='0BT7T'></legend><bdo id='0BT7T'><pre id='0BT7T'><center id='0BT7T'></center></pre></bdo></b><th id='0BT7T'></th></span></q></dt></tr></i><div id='0BT7T'><tfoot id='0BT7T'></tfoot><dl id='0BT7T'><fieldset id='0BT7T'></fieldset></dl></div>
              <tbody id='0BT7T'></tbody>
              <bdo id='0BT7T'></bdo><ul id='0BT7T'></ul>

              <legend id='0BT7T'><style id='0BT7T'><dir id='0BT7T'><q id='0BT7T'></q></dir></style></legend>
            • <tfoot id='0BT7T'></tfoot>

              <small id='0BT7T'></small><noframes id='0BT7T'>

                  本文介绍了用于 Mongodb 数据存储的 asp.net 核心中基于简单令牌的身份验证/授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要实现非常简单的身份验证机制,基本上有 2 个角色:OwnersUsers.而且我认为拥有 Enum 就足够了.应用程序本身是带有通过 Asp.net 核心实现的 webapi 的 SPA.我看到了文章 - 如何使用 EF Identity 实现它,但他们的模型看起来比我实际需要的要复杂得多,而且 EF 面向 SQL db,我使用的是 mongo.所以我的用户看起来像:

                  I need to implement pretty simple auth mechanizm with basically 2 roles: Owners and Users. And I think that having Enum for that will be enough. App itself is SPA with webapi implemented via Asp.net core. I saw article - how to implement it using EF Identity, but their models looks much more complex than I actually need and EF oriented to SQL db, and I using mongo. So my user will looks something like:

                  class UserModel{
                      Id, 
                      Token, 
                      Roles: ["Owners", "Users"],
                      ...
                  }
                  

                  那么我需要实现哪些接口并将其添加到 DI 才能使用[Authorize][Authorize(Roles="Users")] 属性并且它们根据我在标头中发送的令牌正常工作?

                  So what interfaces I need to implement and add to DI to be able use [Authorize] and [Authorize(Roles="Users")] attribute and they worked correctly based on token I send in header?

                  推荐答案

                  让我澄清一点@Adem 的答案.您需要以特定方式实现自定义中间件.实现这一点需要实现 3 个抽象类(答案对于 asp.net core rc2btw 是正确的):

                  Let me clarify a little @Adem's answer. You need to to implement custom middleware in specific way. There is 3 abstract classes that need to be implemented to implementing this (answer is correct for asp.net core rc2btw):

                  Microsoft.AspNetCore.Builder.AuthenticationOptionsMicrosoft.AspNetCore.Authentication.AuthenticationMiddlewareMicrosoft.AspNetCore.Authentication.AuthenticationHandler

                  然后将此中间件添加到您的启动类中.

                  and then add this middleware to your startup class.

                  代码示例:

                  public class TokenOptions : AuthenticationOptions
                      {
                          public TokenOptions() : base()
                          {
                              AuthenticationScheme = "Bearer";
                              AutomaticAuthenticate = true;
                          }
                      }
                  
                  public class AuthMiddleware : AuthenticationMiddleware<TokenOptions>
                  {
                      protected override AuthenticationHandler<TokenOptions> CreateHandler()
                      {
                         return new AuthHandler(new TokenService());
                      }
                  
                      public AuthMiddleware(RequestDelegate next, IOptions<TokenOptions> options, ILoggerFactory loggerFactory, UrlEncoder encoder) : base(next, options, loggerFactory, encoder)
                      {
                      }
                  }
                  
                  public class AuthHandler : AuthenticationHandler<TokenOptions>
                  {
                      private ITokenService _tokenService;
                  
                      public AuthHandler(ITokenService tokenService)
                      {
                          _tokenService = tokenService;
                      }
                  
                      protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
                      {
                          string token = null;
                          AuthenticateResult result = null;
                          string token = Helper.GetTokenFromHEader(Request.Headers["Authorization"]);
                          // If no token found, no further work possible
                          if (string.IsNullOrEmpty(token))
                          {
                              result = AuthenticateResult.Skip();
                          }
                          else
                          {
                              bool isValid = await _tokenService.IsValidAsync(token);
                              if (isValid)
                              {
                                  //assigning fake identity, just for illustration
                                  ClaimsIdentity claimsIdentity = new ClaimsIdentity("Custom");
                                  var claims = new List<Claim>();
                                  claims.Add(new Claim(ClaimTypes.Name, "admin"));
                                  claims.Add(new Claim(ClaimTypes.NameIdentifier, "admin"));
                                  claims.Add(new Claim(ClaimTypes.Role, "admin"));
                                  ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
                                  result =
                                      AuthenticateResult.Success(new AuthenticationTicket(claimsPrincipal,
                                          new AuthenticationProperties(), Options.AuthenticationScheme));
                              }
                              else
                              {
                                  result = AuthenticateResult.Skip();
                              }
                          }
                  
                          return result;
                      }
                  }`
                  

                  附言该代码仅用于说明想法.当然,您需要实现自己的处理程序.

                  这篇关于用于 Mongodb 数据存储的 asp.net 核心中基于简单令牌的身份验证/授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 ASP.NET core rc2 中禁用浏览器缓存? 下一篇:依赖注入与控制器类以外的类

                  相关文章

                  <legend id='ZGhTV'><style id='ZGhTV'><dir id='ZGhTV'><q id='ZGhTV'></q></dir></style></legend>

                  <small id='ZGhTV'></small><noframes id='ZGhTV'>

                    • <bdo id='ZGhTV'></bdo><ul id='ZGhTV'></ul>
                  1. <i id='ZGhTV'><tr id='ZGhTV'><dt id='ZGhTV'><q id='ZGhTV'><span id='ZGhTV'><b id='ZGhTV'><form id='ZGhTV'><ins id='ZGhTV'></ins><ul id='ZGhTV'></ul><sub id='ZGhTV'></sub></form><legend id='ZGhTV'></legend><bdo id='ZGhTV'><pre id='ZGhTV'><center id='ZGhTV'></center></pre></bdo></b><th id='ZGhTV'></th></span></q></dt></tr></i><div id='ZGhTV'><tfoot id='ZGhTV'></tfoot><dl id='ZGhTV'><fieldset id='ZGhTV'></fieldset></dl></div>
                    1. <tfoot id='ZGhTV'></tfoot>