Cra*_*aig 6 c# asp.net authentication asp.net-web-api knockout.js
在使用OWIN使用基于令牌的身份验证的在线教程之后,我设法让我的测试应用程序根据硬编码的用户名/密码进行身份验证,正如演示所做的那样.
但是,现在我希望使用我的Web应用程序中的模型.
正如演示所说,我的身份验证发生在这段代码中.
namespace UI
{
public class AuthorisationServerProvider : OAuthAuthorizationServerProvider
{
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated(); // Means I have validated the client.
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
// Here we validate the user...
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
if (context.UserName == "user" && context.Password == "password")
{
identity.AddClaim(new Claim(ClaimTypes.Role, "admin"));
identity.AddClaim(new Claim("username", "user"));
identity.AddClaim(new Claim(ClaimTypes.Name, "My Full Name"));
context.Validated(identity);
}
else
{
context.SetError("Invalid grant", "Username or password are incorrect");
return;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个WebAPI控制器,我收到了一个模型,并且...不确定如何从我的webapi控制器调用上面的代码.目前,上面的代码需要调用myurl/token - 这是在启动代码中定义的.
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Enables cors origin requests.
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
// Config OAuth authorisation server;
var myProvider = new AuthorisationServerProvider();
OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true, // Live version should use HTTPS...
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = myProvider
};
app.UseOAuthAuthorizationServer(options);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
}
}
Run Code Online (Sandbox Code Playgroud)
所以,我猜我的webapi调用的url应该是/ token吗?所以,在我的UI上的我的(Knockout View模型)代码中,我尝试了这个:
Login()
{
var data = {
username : this.login.emailAddress(),
password : this.login.password(),
RememberMe: this.login.rememberMe(),
grant_type: "password"
}
return $.ajax({
type: "POST",
data: data ? JSON.stringify(data) : null,
dataType: "json",
url: "/token",
contentType: "application/json"
}).done((reply) => {
alert("Done!");
});
}
Run Code Online (Sandbox Code Playgroud)
但是,我得到一个例外:
“error”: “unsupported_grant_type”
Run Code Online (Sandbox Code Playgroud)
在'Postman'中,我能够验证硬编码的用户名/密码.
但我不知道如何从我的UI连接我的api调用,以进行身份验证.
我希望在我的api控制器(ASP.Net WebAPI)上创建一个'Login'方法,如下所示:
[Route("login"), HttpPost, AllowAnonymous]
public ReplyDto Login(LoginRequest login)
{
ReplyDto reply = _userService.Login(login.Email, login.Password);
return reply;
}
Run Code Online (Sandbox Code Playgroud)
所以,我的_userService检查用户是否在数据库中......如果是,请在此处调用我的OAuth身份验证,传递一些参数.但不确定这是可能的.我可以通过这种api方法调用我的身份验证吗?我需要删除/ token位.
因为您已经拥有它,所以不需要创建Login方法.它是http:// localhost:1234/token.如果用户存在且密码正确,则会生成令牌.但是要获得这种行为,您需要通过派生OAuthAuthorizationServerProvider来实现自己的AuthServerProvider
public class DOAuthServerProvider : OAuthAuthorizationServerProvider
Run Code Online (Sandbox Code Playgroud)
然后你将覆盖一个方法来实现你的逻辑:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
try
{
string allowedOrigin = context.OwinContext.Get<string>(DOAuthStatic.ALLOWED_CORS_ORIGINS_KEY);
if (allowedOrigin != null)
{
context.OwinContext.Response.Headers[DOAuthStatic.CORS_HEADER] = allowedOrigin;
}
DAuthenticationResponse authResponse = await _authRepository.Authenticate(context.UserName, context.Password);
if (!authResponse.IsAuthenticated)
{
context.SetError(OAuthError.InvalidGrant, $"{(int)authResponse.AuthenticateResult}:{authResponse.AuthenticateResult}");
return;
}
if (authResponse.User.ChangePasswordOnLogin)
{
_userAuthenticationProvider.GeneratePasswordResetToken(authResponse.User);
}
IDictionary<string, string> props = new Dictionary<string, string>
{
{
DOAuthStatic.CLIENT_NAME_KEY, context.ClientId ?? string.Empty
}
};
ValidateContext(context, authResponse, props);
}
catch (Exception ex)
{
DLogOAuth.LogException(ex, "DCO0407E", "OAuthServerProvider - Error validating user");
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
你几乎就在那里,你只需再做两个步骤:
这是一系列很好的教程,可以很好地解释一切:基于令牌的身份验证(比我更好:) :)
| 归档时间: |
|
| 查看次数: |
3453 次 |
| 最近记录: |