【问题标题】:Bearer Token for angular in asp.net web apiasp.net web api中角度的不记名令牌
【发布时间】:2018-01-24 07:23:30
【问题描述】:

场景很简单 为了简单起见,我使用自定义登录来为 Angular 生成不记名令牌我已经对 google 进行了研究,但我想知道为什么在这个问题上没有简单的例子

下面是代码:

 public class IdentityController : ApiController
    {
        IdentityController()
        {
            OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
        }
        public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; private set; }
        [HttpPost]
        public IHttpActionResult login([FromBody]LoginModel user)
        {
            var username = user.Username.ToLower();
            var password = user.Password;

            if (username == "admin" && password == "1234")
            {
                var identity = new ClaimsIdentity(username);
                identity.AddClaim(new Claim(ClaimTypes.Name, username));
                var principal = new ClaimsPrincipal(identity);
                Thread.CurrentPrincipal = new ClaimsPrincipal(principal);
                if (HttpContext.Current != null)
                {
                    HttpContext.Current.User = principal;
                }

                var ticket = new AuthenticationTicket(identity, new AuthenticationProperties());
                var currentUtc = new SystemClock().UtcNow;
                ticket.Properties.IssuedUtc = currentUtc;
                ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromDays(30));



                FormsAuthentication.SetAuthCookie(username , false);
                var result = new { authenticatonToke = OAuthBearerOptions.AccessTokenFormat.Protect(ticket) };
                return Ok(result);
            }

            else return BadRequest("wrong user name or password");
        }

    }

我得到空引用错误

OAuthBearerOptions.AccessTokenFormat.Protect(ticket)

因为 AccessTokenFormat 为空

是否有任何其他可能的解决方法来在 Web API 中生成不记名令牌? 我不想使用 asp.net 身份或任何其他东西..只要 admin 和 password 是 1234 。当我得到不记名令牌时,我想在邮递员中使用它

【问题讨论】:

标签: asp.net-web-api asp.net-web-api2


【解决方案1】:

我强烈建议您使用Identity Server 来生成令牌、管理用户/客户端等。您还可以在此page 中找到几个 OpenId 认证的解决方案

联合安全是一个重要的话题。内部解决方案可能会遇到第三方问题。

【讨论】:

  • 我知道,这只是为了测试的目的。安全性不在我正在构建的应用范围内。
  • 在他们的 github 页面上花一些时间可能是值得的。 TokenResponseGenerator.cs 可能对您的问题有所帮助。
  • This Library 也可以帮助您创建加密令牌。
猜你喜欢
  • 1970-01-01
  • 2014-07-28
  • 2023-03-11
  • 2018-05-06
  • 2014-06-19
  • 1970-01-01
  • 2019-09-20
  • 1970-01-01
  • 2020-12-10
相关资源
最近更新 更多