我想要一个非常简单的身份验证在ASP.net 5ASP.net 5 Web API 2密码验证
所有你需要进行身份验证是一个密码。没有UserManager或任何这种事情。
如何登录行动看起来像这样或我该如何着手实现呢?
编辑: 密码是一个全局密码,并非针对每个用户。
我想要一个非常简单的身份验证在ASP.net 5ASP.net 5 Web API 2密码验证
所有你需要进行身份验证是一个密码。没有UserManager或任何这种事情。
如何登录行动看起来像这样或我该如何着手实现呢?
编辑: 密码是一个全局密码,并非针对每个用户。
我没有测试此所以它只是一个起点。 你需要实现一个自定义的AuthenticationHandler。
喜欢的东西:
class PasswordAuthenticationHandler : AuthenticationHandler<PasswordAuthenticationOptions>
{
public PasswordAuthenticationHandler (PasswordAuthenticationOptionsoptions)
{
//set fields needed from options
}
protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
{
//get the password out of the Request and create claims collection
...
var claimsId = new ClaimsIdentity(claims, Options.AuthenticationType);
return new AuthenticationTicket(claimsId, new AuthenticationProperties());// can use AuthenticationProperties to set additional propertiues needed in ticket
}
// override ApplyResponseChallengeAsync
}
创建从AuthenticationOptions继承PasswordAuthenticationOptions。
创建OWIN中间件
public class PasswordAuthenticationMiddleware : AuthenticationMiddleware<PasswordAuthenticationOptions>
{
public delegate Task<IEnumerable<Claim>> CredentialValidationFunction(string id, string secret);
public PasswordAuthenticationMiddleware(OwinMiddleware next, PasswordAuthenticationOptions options)
: base(next, options)
{}
protected override AuthenticationHandler<PasswordAuthenticationOptions> CreateHandler()
{
return new PasswordAuthenticationHandler(Options);
}
}
然后在Startup.cs你会使用它想:
app.Use<PasswordAuthenticationMiddleware>(options);
你应该能够锁定下来的东西与授权的类,方法或属性添加它作为一个全局过滤器。
我建议你看看:
http://leastprivilege.com/2015/10/12/the-state-of-security-in-asp-net-5-and-mvc-6-authorization/
https://github.com/leastprivilege/AspNet5AuthorizationPlayground/tree/master/src/Authentication
你可以尝试实现最简单的方法:API控制器的动作里面
Authorization