2017-08-16 8 views
0

シナリオはとてもシンプルです ベアラートークンを生成するための簡単なログインのためにカスタムログインを使用していますが、私は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である場合です。私は郵便配達

+0

これはhttp://odetocode.com/blogs/scott/archive/2015/01/15/using-json-web-tokens-with-katana-and-webapi.aspxがAccessTokenFormatをセットアップするのに役立つかもしれない – jps

答えて

0

でそれを使用したいベアラートークンを取得するとき、私は強くあなたはまた、このpage

にいくつかのOpenIDの認証を取得し解決策を見つけることができ、ユーザ/クライアントなどを管理し、トークンを生成するために Identity Serverを使用することをお勧めいたしますでしょう

フェデレーションセキュリティは重要なトピックです。社内のソリューションには、第三者に問題がある可能性があります。

+0

知っている、それはちょうどテストの一掃のためです。セキュリティは私が構築しているアプリケーションの範囲にはありません。 – Arash

+0

githubページで時間を過ごす価値があります。 TokenResponseGenerator.csがあなたの質問に役立つかもしれません。 – zapoo

+0

[このライブラリ](https://github.com/dvsekhvalnov/jose-jwt)は、暗号化トークンの作成にも役立ちます。 – zapoo