シナリオはとてもシンプルです ベアラートークンを生成するための簡単なログインのためにカスタムログインを使用していますが、私はGoogleの研究を行っていますが、この問題は、以下のasp.netのangerのベアラートークンweb api
上の簡単な例では、コードです:
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)
にnull参照エラーを取得AccessTokenFormat I理由s null
web APIにベアラトークンを生成する可能性のある他の回避策はありますか? 私はasp.netアイデンティティまたは他の何かを使用したくない..単に管理者とパスワードが1234である場合です。私は郵便配達
これはhttp://odetocode.com/blogs/scott/archive/2015/01/15/using-json-web-tokens-with-katana-and-webapi.aspxがAccessTokenFormatをセットアップするのに役立つかもしれない – jps