- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 Owin + Oauth2 + Identity2。
我有一个带有默认基本身份验证设置的 web Api,我已对其进行修改。
我的 startup.cs 部分类
public void ConfigureAuth(IAppBuilder app)
{
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);//TODO: prob wont need this
// Configure the application for OAuth based flow
PublicClientId = "self";
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),//TODO: prob wont need this
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
// In production mode set AllowInsecureHttp = false
AllowInsecureHttp = true //TODO: set debug mode
};
// Token Generation
app.UseOAuthBearerTokens(OAuthOptions);
}
我的 startup.cs 类部分在根
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
ConfigureAuth(app);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
我的应用程序OAuthProvider.cs
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
//get user
var service = new CarrierApi.CarrierManagementClient();
var result = service.LoginAsync(context.UserName, context.Password);
var user = result.Result.Identity;
//TODO: log stuff here? i.e lastlogged etc?
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
ClaimsIdentity oAuthIdentity = user;
ClaimsIdentity cookiesIdentity = user;
AuthenticationProperties properties = CreateProperties(user.GetUserName());
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
如您所见,我实际上是通过对现有数据库的 wcf 调用来获取身份的。使用 postman 时,我获取/token url 并获取我的不记名 token ,在下一个请求中,我将其传递到 header 中并调用我的 Controller 方法。
[Authorize(Roles = "Templates_Access")]
public string Post([FromBody]string value)
{
return "woo";
}
这很好用,如果用户有权限,它不会允许访问,如果他们这样做了。
但是,如果我访问我们使用相同 wcf 和 DB 的网站并更改用户权限,如果我向 postman 发送相同的请求,它仍然允许访问,即使我也删除了对分配给用户的角色的权限。
我如何确保在每次请求时都“刷新”或再次检查权限?
最佳答案
登录用户的每个角色都在登录时作为声明存储在持有者 token 中,在 GrantResourceOwnerCredentials 方法中。如果必须授权请求,则通过 AuthorizationFilter 的默认实现在不记名 token 中存储的列表中搜索角色;因此,如果您更改用户的权限,则需要重新登录。
这种行为尊重 Restfull 架构的无状态约束,正如 Fielding 在他的 dissertation 中所写的那样, 这也是性能和安全性之间的一个很好的平衡
如果您需要不同的行为,则有不止一种可能性。
刷新 token
您可以使用 Refresh Token,实现 applicationOAuthProvider 类的 GrantRefreshToken 方法;您可以检索刷新的用户权限并创建新的访问 token ;这是一篇学习的好文章how .
请记住:
检查每个请求的权限
您可以实现自定义 AuthorizationFilter 并在数据库中检查用户的权限,但这是一个缓慢的解决方案。
缓存和登录 session
您可以在 GrantResourceOwnerCredentials 方法中为每次登录生成用户 session 的 key (如 guid),并将其作为声明存储在不记名 token 中。您还必须使用两个索引将其存储在缓存系统(如 Redis)中:用户 session 的 key 和 userId。 Redis官方文档解释how .
当一个用户的权限改变时,你可以在缓存系统中使该用户的每个 session 失效,通过userId搜索
您可以实现自定义 AuthorizationFilter 并检查缓存中的每个请求是否有效,通过用户 session 的 key 进行搜索。
小心:这将违反无状态约束,您的架构将不会是 restfull
在这里您可以找到 AuthorizaAttribute filter 的标准实现.您可以创建自定义过滤器,扩展 AuthorizeAttribute 并覆盖 IsAuthorized 方法。
很可能还有其他方法,但是更改用户权限的频率如何?在许多系统中,也是在安全性是第一要求的系统中,如果在事件 session 期间更改了用户的权限配置,则需要重新登录才能激活新登录。您确定需要修改此标准行为吗?
如果你是,我建议使用缓存系统的解决方案。
关于c# - Web API2 identity2 承载 token 权限更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45103130/
SO亲爱的 friend 们: 2014 年 3 月 18 日。我正在处理一种情况,在使用 ng-repeat 时,数组内的元素(我从 Json 字符串中获取)更改了原始顺序。 需要明确的是,数组中的
有很多问题询问如何在 JavaScript 单击处理程序中更改 div 的类,例如,此处:Change Div style onclick .我理解得很好(只需更改 .className),并且它有效
我从access导入了一个数据库到mysql,但其中一个表的列名“股数”带有空格,但我尝试更改、替换甚至删除列名,但失败了。任何人都可以帮助解决这一问题 String UpdateQuary = "U
我正在做一个随机的学校元素。 目前,我有一个包含两个 CSS 的页面。一种用于正常 View ,一种用于残障人士 View 。 此页面还包括两个按钮,它们将更改使用的样式表。 function c
我需要使用 javascript 更改 HTML 元素中的文本,但我不知道该怎么做。 ¿有什么帮助吗? 我把它定义成这样: Text I want to change. 我正在尝试这样做: docum
我在它自己的文件 nav_bar.shtml 中有一个主导航栏,每个其他页面都包含该导航栏。这个菜单栏是一个 jQuery 菜单栏(ApyCom 是销售这些导航栏的公司的名称)。导航栏上的元素如何确定
我正在摆弄我的代码,并开始想知道这个变化是否来自: if(array[index] == 0) 对此: if(!array[index] != 0) 可能会影响任何代码,或者它只是做同样的事情而我不需
我一直在想办法调整控制台窗口的大小。这是我正在使用的函数的代码: #include #include #define WIDTH 70 #define HEIGHT 35 HANDLE wHnd;
我有很多情况会导致相同的消息框警报。 有没有比做几个 if 语句更简单/更好的解决方案? PRODUCTS BOX1 BOX2 BOX3
我有一个包含这些元素的 XELEMENT B Bob Petier 19310227 1 我想像这样转换前缀。 B Bob Pet
我使用 MySQL 5.6 遇到了这种情况: 此查询有效并返回预期结果: select * from some_table where a = 'b' and metadata->>"$.countr
我想知道是否有人知道可以检测 R 中日期列格式的任何中断的包或函数,即检测日期向量格式更改的位置,例如: 11/2/90 12/2/90 . . . 15/Feb/1990 16/Feb/1990 .
我希望能够在小部件显示后更改 GtkButton 的标签 char *ButtonStance == "Connect"; GtkWidget *EntryButton = gtk_button_ne
我正在使用 Altera DE2 FPGA 开发板并尝试使用 SD 卡端口和音频线路输出。我正在使用 VHDL 和 C 进行编程,但由于缺乏经验/知识,我在 C 部分遇到了困难。 目前,我可以从 SD
注意到这个链接后: http://www.newscientist.com/blogs/nstv/2010/12/best-videos-of-2010-progress-bar-illusion.h
我想知道在某些情况下,即使剧本任务已成功执行并且 ok=2,ansible 也会显示“changed=0”。使用 Rest API 和 uri 模块时会发生这种情况。我试图找到解释但没有成功。谁能告诉
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: add buttons to push notification alert 是否可以在远程通知显示的警报框中指定有
当您的 TabBarController 中有超过 5 个 View Controller 时,系统会自动为您设置一个“更多” View 。是否可以更改此 View 中导航栏的颜色以匹配我正在使用的颜
如何更改.AndroidStudioBeta文件夹的位置,默认情况下,该文件夹位于Windows中的\ .. \ User \ .AndroidStudioBeta,而不会破坏任何内容? /编辑: 找
我目前正在尝试将更具功能性的编程风格应用于涉及低级(基于 LWJGL)GUI 开发的项目。显然,在这种情况下,需要携带很多状态,这在当前版本中是可变的。我的目标是最终拥有一个完全不可变的状态,以避免状
我是一名优秀的程序员,十分优秀!