cookie認証を使用して自分のasp.netアプリケーションを設定する際に助けが必要です。これは私の設定は次のようになります。[Authorize]属性の使用時に認証Cookieが読み取られない
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
CookieSecure = CookieSecureOption.SameAsRequest,
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
PublicClientId = "self";
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
app.UseOAuthBearerTokens(OAuthOptions);
}
マイログインAPIのルートがある:私はログアウトアクションを呼び出すときにログインを呼び出し
[Route("Login")]
[HttpPost]
[AllowAnonymous]
public IHttpActionResult Login(RegisterBindingModel model)
{
var user = UserManager.Find(model.Username, model.Password);
if (user != null)
{
Authentication.SignOut();
var identity = UserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
identity.AddClaim(new Claim(ClaimTypes.Role, "IsAdmin"));
Authentication.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);
return Ok("Success");
}
return Ok();
}
は.AspNet.ApplicationCookieという名前のCookieを返しますが:
[Route("Logout")]
[HttpPost]
public IHttpActionResult Logout()
{
Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationType);
return Ok();
}
次のエラーが表示されます。Authorizaこのリクエストで拒否されました
私は間違っていますか?
注:私は[承認]とコントローラを飾っ属性
MVCとWebAPIの2つのプロジェクトがありますか?その場合は、私の答えをここで確認してください - http://stackoverflow.com/questions/38424518/use-web-api-cookie-for-mvc-cookie/38428420#38428420 。 BTWは、ログアウトまたは[Authorize]属性で装飾されたすべてのコントローラの問題です。 –
あなたのコメントは、自分のWeb API設定を見て、ベアラトークンを許可するようにのみ設定されていることを認識しました。私はSuppressDefaultHostAuthenticationへの呼び出しを削除し、すべて正常に動作します。正しい方向に私を指してくれてありがとう。 – Draco
ああ、デフォルトのテンプレートには常にそれがあります。あなたが考え出してうれしい。 –