【发布时间】:2017-07-14 12:34:13
【问题描述】:
使用 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 调用来获取身份的。使用邮递员时,我获取 /token url 并获取我的不记名令牌,在下一个请求中,我将其传递到标头并调用我的控制器方法。
[Authorize(Roles = "Templates_Access")]
public string Post([FromBody]string value)
{
return "woo";
}
这很好用,如果用户有权限,它不会允许访问,如果他们允许的话。
但是,如果我访问使用相同 wcf 和 DB 的网站并更改用户权限,如果我在邮递员上发送相同的请求,它仍然允许访问,即使我删除了对该用户分配的角色的权限。
如何确保在每个请求上“刷新”或再次检查权限?
【问题讨论】:
-
您想为每个发出的请求调用 WCF 服务吗?
-
好吧,我需要检查在数据库上设置的权限,wcf 是我们数据库的唯一访问权限。基本上我需要一种方法来检查权限没有改变,如果他们已经更新了会话或任何授权工作
标签: c# oauth-2.0 asp.net-web-api2 asp.net-identity-2