【发布时间】: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 。当我得到不记名令牌时,我想在邮递员中使用它
【问题讨论】:
-
this:odetocode.com/blogs/scott/archive/2015/01/15/… 可能会帮助您设置 AccessTokenFormat
标签: asp.net-web-api asp.net-web-api2