您的密码的grant_type将是“密码”它自己。例如,从我的应用程序中拿这个例子:
JAVASCRIPT - 使用角度与OAuth2.0和c#WebAPI 2.0(MVC)的Auth服务 - 使用的模式。 - API
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var source = LogUtility.InitializeContext(this.GetType());
try
{
var allowedOrigin = context.OwinContext.Get<String>("clientAllowedOrigin") ?? "*";
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });
var signOnContext = await new AccessControlBusinessLogicLayer().LoadUserSignOnContextOrDefaultByCredentials(context.Options.AuthenticationType, context.UserName, context.Password);
if (signOnContext == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
context.OwinContext.Set<String>("userSecurityStamp", signOnContext.SecurityStamp);
var authenticationProperties = new AuthenticationProperties(
new Dictionary<String, String>
{
{ "client_id", context.ClientId }
});
var authenticationTicket = new AuthenticationTicket(signOnContext.Identity, authenticationProperties);
if (context.Scope[0].ToLower() == "website")
{
using (var userContext = new AccessControlContext())
{
using (var userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(
var user = AccessBL.LoadUserOrDefaultByUserName(context.UserName);
var userId = user.Id;
}
}
}
context.Validated(authenticationTicket);
}
catch (Exception exception)
{
throw exception;
}
}
这将检查授权并为当前上下文
C#:
return $http({
url: $rootScope.globals.apiPath + "/someController/token",
method: 'POST',
data: "userName=" + encodeURIComponent(username) +
"&password=" + encodeURIComponent(password) +
"&Scope=" + "website" +
"&grant_type=password" + // here I pass the type as password to the web api
"&client_id=clientIdExample",
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
这是然后在API使用授权的资源所有者凭证像这样处理当前用户。希望这有助于解决模糊问题。这意味着密码授予将是来自Web客户端的授予资源,而不是隐含的。
嗯,我宁愿在mvc应用程序登录过程(用户名+密码)期间请求承载认证令牌,然后将认证令牌(承载)发送回浏览器客户端。然后,持票人令牌随后通过使用jQuery Ajax的Authorization Header在每个请求上发送给承载安全的WebApi。当使用密码授权时,这将是我想象的过程。对?因为我不想将用户凭据传递给浏览器,然后传递给资源服务器。 – Legends
专注于这一点正是您需要做的,您只需通过请求的标头将承载发送到安全的web api,并且您绝对不希望将用户凭证来回传递给浏览器,这会使其承受安全风险。所以从我在流程图中可以看到的情况来看,如果您使用“密码”作为授予类型,那么您应该朝着正确的方向前进。而且我也对此进行了大量研究,并提出了使用此解决方案的最佳实践:http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api -2-owin-asp-net-identity/ – ThatAwesomeCoder
但在你的例子中,你使用凭证通过angular.js在浏览器中运行来调用令牌服务端点...?! – Legends