No, you have an overall session timeout that needs to be as long as the longest lifetime you want for a session variable. You could add some code to shorten the lifetime for some of them but I believe this is not your issue?
Or if the issue is authentication (if I remember you are using a session variable) you are running into a problem I alluded earlier ie by using a session variable you tied now the authencation duration with the lifetime of those session variables.
Edit: especially for security related code it's likely best to use what comes out of the box. For now you could just ignore what you are not using and I'll give a look at using only what one needs...
Since you are writing a custom feature and not following standards, it is your responsibility to design and write the code. Pretty simply though just add another Session value that has the timeout. Then check the time out when accessing the value. Or,
as recommended above, create an object model that includes the value and the timeout. There are many ways to solve this basic programming problem.
public class CustomRoleWithTimeout
{
public string Role { get; set; }
public DateTime? Timeout { get; set; }
public static bool HasTimedOut(CustomRoleWithTimeout obj)
{
return obj.Timeout.HasValue && obj.Timeout.Value < DateTime.Now;
}
}
Member
92 Points
534 Posts
Can I change session variable's timeout individually?
Oct 06, 2019 06:48 AM|fatihbarut|LINK
Hi all,
I change session timeout in startup using the code below
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(100);
});
but how can I change timeout of session variables individually? if possible.
Participant
1968 Points
1026 Posts
MVP
Re: Can I change session variable's timeout individually?
Oct 06, 2019 07:57 AM|maherjendoubi|LINK
Hi,
Would you please eloborate more by giving concrete examples?
Best regards,
Maher
Blog : https://maherjendoubi.io
Member
92 Points
534 Posts
Re: Can I change session variable's timeout individually?
Oct 06, 2019 08:03 AM|fatihbarut|LINK
ı meant can we do this
HttpContext.Session.SetString("Role", "Updater");
HttpContext.Session.timeout("Role", 100); (as minutes)
All-Star
58464 Points
15782 Posts
Re: Can I change session variable's timeout individually?
Oct 07, 2019 12:16 AM|bruce (sqlwork.com)|LINK
You will need to implement yourself. Just store the value as an object with a value and a timeout.
Member
92 Points
534 Posts
Re: Can I change session variable's timeout individually?
Oct 07, 2019 07:30 AM|fatihbarut|LINK
thanks but any code piece?
All-Star
48710 Points
18184 Posts
Re: Can I change session variable's timeout individually?
Oct 07, 2019 08:04 AM|PatriceSc|LINK
Hi,
No, you have an overall session timeout that needs to be as long as the longest lifetime you want for a session variable. You could add some code to shorten the lifetime for some of them but I believe this is not your issue?
Or if the issue is authentication (if I remember you are using a session variable) you are running into a problem I alluded earlier ie by using a session variable you tied now the authencation duration with the lifetime of those session variables.
Edit: especially for security related code it's likely best to use what comes out of the box. For now you could just ignore what you are not using and I'll give a look at using only what one needs...
All-Star
53661 Points
24020 Posts
Re: Can I change session variable's timeout individually?
Oct 07, 2019 03:06 PM|mgebhard|LINK
If this is a continuation from your previous thread;https://forums.asp.net/p/2160399/6280466.aspx?Re+How+can+I+simplify+net+core+authentication. Be aware the recommend and industry standard approach of using an auth cookie has this ability built-in. The token contained within the auth cookie has a timeout which can be set to whatever you like.
Since you are writing a custom feature and not following standards, it is your responsibility to design and write the code. Pretty simply though just add another Session value that has the timeout. Then check the time out when accessing the value. Or, as recommended above, create an object model that includes the value and the timeout. There are many ways to solve this basic programming problem.