使用Owin + Oauth2 + Identity2。Web API2身份2承載令牌許可更改
我有一個網絡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);
}
我applicationOAuthProvider.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調用來獲取身份。當使用郵遞員時,我得到/令牌url並獲得我的不記名令牌,在下一個請求中,我將它傳遞給頭並調用我的控制器方法。
[Authorize(Roles = "Templates_Access")]
public string Post([FromBody]string value)
{
return "woo";
}
這很好,如果用戶有權限,它不會允許訪問,如果他們這樣做。
但是,如果我去我們的網站,使用相同的wcf和數據庫,並更改用戶權限,如果我發送郵遞員相同的請求它仍然允許訪問,即使我也刪除了該用戶分配的角色的權限。
我如何確保在每次請求時「刷新」或再次檢查權限?
您想調出您的WCF服務以獲取每個請求嗎? – DavidG
以及我需要檢查權限,這是在數據庫上設置的,WCF是唯一的訪問我們的數據庫。基本上我需要一種方式來檢查權限沒有改變,如果他們已然後更新會話或任何其他事情,以便授權工作 – lemunk