我有以下设置。使用身份和令牌授权的Web API,包含整个用户管理,当然还有一些额外的API调用。
此外,我正在构建一个FE来再次使用.net核心的应用程序接口。我有我的登录表和post到FE,它使用RestSharp来做一个应用程序接口请求,给出我从表单获得的凭据。并且我接收一些用户数据和身份Cookie/令牌。我将cookie/token设置为响应,现在我的FE可以使用它来执行其他API调用,例如ajax。
但我的问题是,我如何知道他在X分钟后仍在登录?如果Cookie/token过期,API调用将被拒绝,但我的FE-BE如何知道它们不再有效?我是否要检查每个请求的到期日期?

我问这个主要是为了挑战我构建系统的方式,以避免任何巨大的安全缺陷。
发布于 2016-11-05 07:56:49
如果Cookie/token过期,应用程序接口调用将被拒绝,但我的FE-BE如何知道它们不再有效?我是否要检查每个请求的到期日期?
要确定授权访问是否已过期,请检查optionally comes with an OpenID Connect implicit flow response.的expires_in时间范围。
access_tokentoken_typeid_tokenstateexpires_in可选。访问令牌自生成响应以来的过期时间。由于access_token对客户端通常是不透明的,因此确定访问权限是否已过期的唯一另一种方法是询问access_token的颁发者。
但我的问题是,我如何知道他在X分钟后仍在登录?
要确定您的用户是否仍在登录,请使用id_token而不是access_token。
发布于 2016-11-05 08:12:37
在与朋友交谈后,(在我看来)更合适的解决方案如下所示。(代码可以清理)
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
// Do a rest call to the API
Uri _baseUri = new Uri("http://localhost:8000/");
var client = new RestClient(_baseUri + "api/Account/login");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "7ee2a21b-70d5-8a68-f0dd-518b8a61ddbf");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "Email=blah%40gmail.com&password=a1Aa1Aa1A!&=", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
// Check the response
if (response.StatusCode == HttpStatusCode.OK) {
// Grab the cookie for the Identity
// this can be replaced by a token in the future
String cookie = response.Cookies.Where(c => c.Name == ".AspNetCore.Identity.Application").First().Value;
// Store the cookie value to use it in sub-sequent requests
HttpContext.Session.SetString("IdentityCookieId", cookie);
// Add claims to our new user, an example Name and an example Role
const string Issuer = "http://blah.com";
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, "AnonymUser", ClaimValueTypes.String, Issuer));
claims.Add(new Claim(ClaimTypes.Role, "Administrator", ClaimValueTypes.String, Issuer));
var userIdentity = new ClaimsIdentity("SecuredLoggedIn");
userIdentity.AddClaims(claims);
var userPrincipal = new ClaimsPrincipal(userIdentity);
// Sign in the user creating a cookie with X ammount of Expiry
await HttpContext.Authentication.SignInAsync("Cookie", userPrincipal,
new AuthenticationProperties
{
ExpiresUtc = DateTime.UtcNow.AddMinutes(1),
IsPersistent = false,
AllowRefresh = false
});
// Move back to the ReturnUrl or for me always to the dashboard
return RedirectToLocal("/dashboard");
}
}
return View(model);
}当然,您必须编辑ConfigureServices下的Startup.cs文件,以便在AddMvc()之前添加services.AddAuthorization();。
并在Configure下添加
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookie",
LoginPath = new PathString("/account/login/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true
});https://stackoverflow.com/questions/40429647
复制相似问题