asp.net – 如何在.net WebApi2应用程序中使用OAuth2令牌请求中
发布时间:2020-12-15 18:53:15 所属栏目:asp.Net 来源:网络整理
导读:我有一个api特定的项目在一个大的.net MVC 5 web解决方案。我使用开箱即用的WebApi2模板通过api验证用户。使用个人帐户进行身份验证,获取访问令牌所需的请求正文是: grant_type=passwordusername={someuser}password={somepassword} 这按预期工作。 但是,
我有一个api特定的项目在一个大的.net MVC 5 web解决方案。我使用开箱即用的WebApi2模板通过api验证用户。使用个人帐户进行身份验证,获取访问令牌所需的请求正文是:
grant_type=password&username={someuser}&password={somepassword} 这按预期工作。 但是,我需要添加第三个维度到支架方法“GrantResourceOwnerCredentials”。除了检查用户名/密码,我需要添加设备ID,这意味着限制从用户帐户到特定设备的访问。不清楚的是如何将这些额外的请求参数添加到已定义的“OAuthGrantResourceOwnerCredentialsContext”。这个上下文目前为UserName和密码,但显然我需要包括更多的空间。 我的问题是,有没有标准的方法来扩展OWIN OAuth2令牌请求的登录要求,以包括更多的数据?而且,你将如何在.NET WebApi2环境中可靠地做到这一点? 解决方法
由于经常是这样,我在提交问题后立即找到答案…
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); } } 通过简单地添加 var data = await context.Request.ReadFormAsync(); 在方法中,您可以访问请求正文中的所有已发布的变量,并根据需要使用它们。在我的情况下,我立即在用户的空检查之后执行更严格的安全检查。 希望这有助于某人! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET压力测试
- asp.net-web-api – WebAPI 2属性路由启用会话状态
- asp.net-mvc – ASP.net MVC 3项目文件没有出现在新项目对话
- asp.net-web-api – 为WCF REST和ASP.NET Web API共享相同的
- asp.net – SignalR跨域不在IE10以外的浏览器上工作
- asp.net-mvc – 自定义EditorTemplate可以为其中的相同模型
- asp.net-mvc – MVC:存储库和服务
- Asp.Net MVC动态模型绑定前缀
- IIS 7中的虚拟目录ASP.NET网站的配置错误
- Asp.net中的Session和Cookie
推荐文章
站长推荐
- asp.net-mvc – 您是否使用任何自定义ASP.NET MV
- asp.net – CookieContainer和Response.Cookies集
- asp.net-mvc – ASP.NET MVC – Elmah不工作,返回
- asp.net-mvc – 找不到Nancy Razor Viewengine
- asp.net – 获取隐藏字段的值
- asp.net – Web API 2 – ApiController.Interna
- asp.net-mvc – MVC 3 tempdata容器的缺点
- 实体框架 – ASP.NET Web Api 2/EF6首次调用初始
- ASP.NET 2.0到4.0
- JObject对象如何获取深度属性值&不判断key是
热点阅读