nak*_*ive 36 asp.net oauth-2.0 asp.net-mvc-5 asp.net-web-api2
我在一个大型.net MVC 5 Web解决方案中有一个api特定项目.我正在利用开箱即用的WebApi2模板通过api对用户进行身份验证.使用个人帐户进行身份验证,获取访问令牌所需的请求正文是:
grant_type=password&username={someuser}&password={somepassword}
Run Code Online (Sandbox Code Playgroud)
这按预期工作.
但是,我需要在scaffolded方法"GrantResourceOwnerCredentials"中添加第三维.除了检查用户名/密码之外,我还需要添加一个设备ID,用于限制从用户帐户到特定设备的访问.目前尚不清楚如何将这些额外的请求参数添加到已定义的"OAuthGrantResourceOwnerCredentialsContext"中.此上下文目前为UserName和Password腾出空间,但显然我需要包含更多内容.
我的问题很简单,是否有一种标准方法可以扩展OWIN OAuth2令牌请求的登录要求以包含更多数据?而且,您如何在.NET WebApi2环境中可靠地做到这一点?
nak*_*ive 94
通常是这样,我在提交问题后立即找到答案......
ApplicationOAuthProvider.cs包含以下开箱即用的代码
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
using (UserManager<IdentityUser> userManager = _userManagerFactory())
{
IdentityUser user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user,
context.Options.AuthenticationType);
ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = CreateProperties(context.UserName, data["udid"]);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
}
Run Code Online (Sandbox Code Playgroud)
只需添加即可
var data = await context.Request.ReadFormAsync();
Run Code Online (Sandbox Code Playgroud)
在方法中,您可以访问请求正文中的所有已发布变量并根据需要使用它们.在我的情况下,我在对用户进行空检查后立即将其放置,以执行更严格的安全检查.
希望这有助于某人!
| 归档时间: |
|
| 查看次数: |
13939 次 |
| 最近记录: |