遵循一個在線教程以使用OWIN使用基於令牌的身份驗證後,我設法讓我的測試應用程序根據硬編碼的用戶名/密碼進行身份驗證,如演示所做的那樣。Asp.Net WebApi 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;
}
}
}
}
我有一個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);
}
}
所以,我猜我的webapi調用的url應該是/ token?所以,在我的UI我的(剔除視圖模型)的代碼,我嘗試這樣做:
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!");
});
}
但是,我得到一個異常:
「error」: 「unsupported_grant_type」
在「郵差」,我能夠認證硬編碼的用戶名/密碼。
但我不知道如何從我的UI線了我的API調用,進行身份驗證。
我希望我的API控制器(ASP.Net的WebAPI)上創建一個「登錄」方法,就像這樣:
[Route("login"), HttpPost, AllowAnonymous]
public ReplyDto Login(LoginRequest login)
{
ReplyDto reply = _userService.Login(login.Email, login.Password);
return reply;
}
所以,我_userService檢查用戶是否在數據庫中...如果是這樣,請在這裏調用我的OAuth身份驗證傳遞幾個參數。但不知道這是可能的。我可以從這個API方法調用我的身份驗證嗎?我需要刪除/令牌位。