私はで新しいですAsp.Netコア。私はJWTベアラトークンベース認証と承認を実装しました。トークンは正常に生成されますが、既存のデータベースでは、AspNetUserテーブルのパスワードは、パスワードハッシュとSecurityStampカラムで暗号化された形式のパスワードを持っています。だから、どのように私はデータベースからユーザー名とパスワードをチェックできますか?PasswordHashとSecurityStampを使用して、既存のデータベースにユーザ名とパスワードをチェックする方法は有効かどうか?
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
ConfigureAuth(app);
app.UseMvc();
}
と上記のコードで
public partial class Startup
{
// The secret key every token will be signed with.
// Keep this safe on the server!
private static readonly string secretKey = "mysupersecret_secretkey!123";
private void ConfigureAuth(IApplicationBuilder app)
{
var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey));
app.UseSimpleTokenProvider(new TokenProviderOptions
{
Path = "/api/token",
Audience = "ExampleAudience",
Issuer = "ExampleIssuer",
SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
IdentityResolver = GetIdentity
});
var tokenValidationParameters = new TokenValidationParameters
{
// The signing key must match!
ValidateIssuerSigningKey = true,
IssuerSigningKey = signingKey,
// Validate the JWT Issuer (iss) claim
ValidateIssuer = true,
ValidIssuer = "ExampleIssuer",
// Validate the JWT Audience (aud) claim
ValidateAudience = true,
ValidAudience = "ExampleAudience",
// Validate the token expiry
ValidateLifetime = true,
// If you want to allow a certain amount of clock drift, set that here:
ClockSkew = TimeSpan.Zero
};
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
TokenValidationParameters = tokenValidationParameters
});
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
AuthenticationScheme = "Cookie",
CookieName = "access_token",
TicketDataFormat = new CustomJwtDataFormat(
SecurityAlgorithms.HmacSha256,
tokenValidationParameters)
});
}
private Task<ClaimsIdentity> GetIdentity(string username, string password)
{
// Here i want to match username and password with passwordHash and SecurityStamp
if (username == "TEST" && password == "TEST123")
{
return Task.FromResult(new ClaimsIdentity(new GenericIdentity(username, "Token"), new Claim[] { }));
}
// Credentials are invalid, or account doesn't exist
return Task.FromResult<ClaimsIdentity>(null);
}
}
は、私はハードコーディングされた値を持つユーザ名とパスワードを確認していますが、私は必要があります。
トークンを生成するための部分的なスタートアップクラスの次のコードを見つけてください。 AspNetUserテーブル(MVC5で自動作成)で既存のデータベースを使用して同じことを実行する(MVC5で自動作成)
ありがとう
jwtとは何が関係していますか?この質問には関係がないようです。 –
電子メールとパスワードを確認した後に返るトークンを生成するJWT –
しかし、トークンの生成に問題はありません。とにかく、セキュリティを実装するために何(パッケージ)を使用したのかなど、情報を追加すると役立ちますか?あなたはusermanagerを実装しましたか?いくつか(適切な)コードを表示できますか? –