asp.net-mvc-5 – Web API 2 OWIN承载令牌的目的cookie?
|
我试图理解在MVC 5中的单页应用程序模板中的新OWIN承载令牌认证过程。请纠正我,如果我错了,对于OAuth密码客户端认证流,承载令牌认证通过检查http授权请求头对于承载访问令牌代码来查看请求是否被认证,它不依赖于cookie来检查特定请求是否被认证。 根据这篇文章: OWIN Bearer Token Authentication with Web API Sample public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
using (IdentityManager identityManager = _identityManagerFactory.CreateStoreManager())
{
if (!await identityManager.Passwords.CheckPasswordAsync(context.UserName,context.Password))
{
context.SetError("invalid_grant","The user name or password is incorrect.");
return;
}
string userId = await identityManager.Logins.GetUserIdForLocalLoginAsync(context.UserName);
IEnumerable<Claim> claims = await GetClaimsAsync(identityManager,userId);
ClaimsIdentity oAuthIdentity = CreateIdentity(identityManager,claims,context.Options.AuthenticationType);
ClaimsIdentity cookiesIdentity = CreateIdentity(identityManager,_cookieOptions.AuthenticationType);
AuthenticationProperties properties = await CreatePropertiesAsync(identityManager,userId);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity,properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
}
GrantReourceOwnerCredentials函数不仅使用以下行来组成票证:context.Validated(ticket);但它还组成一个cookie标识,并将其设置为这条线的cookie:context.Request.Context.Authentication.SignIn(cookiesIdentity); 所以我的问题是,这个函数中cookie的确切目的是什么?认证标签不应该足够好用于认证目的吗? 解决方法在SPA模板中,实际上有两个单独的验证机制启用 – cookie验证和令牌验证。这启用了对MVC和Web API控制器操作的认证,但需要一些额外的设置。如果你看一下WebApiConfig.Register方法,你会看到这行代码: config.SuppressDefaultHostAuthentication(); 这告诉Web API忽略cookie身份验证,这避免了大量的问题,在the link you posted in your question中解释:
因此,现在调用config.SuppressDefaultHostAuthentication()需要授权的Web API调用将忽略自动与请求一起发送的Cookie,并查找以“Bearer”开头的授权标头。 MVC控制器将继续使用cookie认证,并且不知道令牌认证机制,因为它不是一个非常适合网页认证开始。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – MVC 3从web.config中的AppSettings获取值
- asp.net-mvc – 是否可以模拟/伪造扩展方法?
- ASP.NET MVC与ASP.NET 4.0
- asp.net-mvc – 如何在Razor View中格式化lambda表达式中的
- asp.net – 如何访问Microsoft.Owin.Security.xyz OnAuthen
- asp.net – Fulltext Query String的全文查询参数无效
- 验证 – 想知道为什么DisplayName属性在被覆盖属性的LabelF
- asp.net-mvc – ASP.NET MVC与XSL
- ASP.NET标签控件 – 不编码HTML
- 使用ASP.Net MVC3中的jQuery动态填充下拉列表
- 从ASP.NET页面获取请求变量
- asp.net – Request.Browser.Platform不返回iPad
- Asp.Net的FileUpload类实现上传文件实例
- 将ASP.NET TextBox作为HTML5输入类型“Number”
- 从ASP.NET C#启动一个程序
- asp.net-mvc – ASP.NET MVC DropDownListFor不支
- asp.net-mvc – 在MVC中使用ViewModels进行POST操
- asp.net-core – 如何将IHttpContextAccessor注入
- 使用ASP.NET MVC在JS文件中设置jQuery的ajax url
- ASP.NET:WebResource.axd调用404错误:如何知道
