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 – 用于为基于Razor的View编写单元测试的指针
- .net – MVC 5中的“CacheProfile”
- asp.net – 如何从Controller设置@ Html.ActionLink的动态标
- asp.net-mvc – 用于json嵌套数组的MVC3绑定
- asp.net – Page.RouteData.Values对于一个页面是空的而不是
- asp.net-mvc – 如何在Visual Studio 2015中快速解析命名空
- 一个ASP中的Base64加密 解密函数代码
- 如何打印与phpinfo()但ASP.NET类似的信息?
- asp.net – On-Session-expire-event?
- asp.net – 在HTTP POST IIS ASP .NET上出现HTTP 405错误
- asp.net – 使用UpdatePanel的CollectionPager问
- asp.net-mvc – 为什么HttpPostedFile不像广告和
- asp.net – 如何在会话超时或结束时注销用户
- ASP.NET Core 依赖注入基本用法
- asp.net-mvc-routing – ASP.NET 4 MVC Web API:
- 使用ASP.NET将用户订阅到MailChimp
- asp-classic – 在VBScript中检查NULL的错误
- 是否可以在ASP.NET 5的Web API中使用外部身份提供
- asp.net-mvc – Web api自定义路由
- asp.net-mvc – 缓存直到ASP.NET MVC和Entity Fr