1

By using the below code user login successfully to application but logout after 20 minutes. My question is- How can I increase the logout time in Azure or Refresh the token?

Step 1 - Startup.cs

 using System;
    using System.Threading.Tasks;
    using Owin;
    using Microsoft.Owin;
    using Microsoft.IdentityModel.Protocols.OpenIdConnect;
    using Microsoft.IdentityModel.Tokens;
    using Microsoft.Owin.Security;
    using Microsoft.Owin.Security.Cookies;
    using Microsoft.Owin.Security.OpenIdConnect;
    using Microsoft.Owin.Security.Notifications;
    using DocumentFormat.OpenXml.InkML;
    
    [assembly: OwinStartupAttribute(typeof(CookBook.MvcApp.Startup))]
    namespace CookBook.MvcApp
    {
        public partial class Startup
        {
            // The Client ID is used by the application to uniquely identify itself to Azure AD.
            string clientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"];
    
            // RedirectUri is the URL where the user will be redirected to after they sign in.
            string redirectUri = System.Configuration.ConfigurationManager.AppSettings["RedirectUri"];
    
            // Tenant is the tenant ID (e.g. contoso.onmicrosoft.com, or 'common' for multi-tenant)
            static string tenant = System.Configuration.ConfigurationManager.AppSettings["Tenant"];
    
            // Authority is the URL for authority, composed by Microsoft identity platform endpoint and the tenant name (e.g. https://login.microsoftonline.com/contoso.onmicrosoft.com/v2.0)
            string authority = String.Format(System.Globalization.CultureInfo.InvariantCulture, System.Configuration.ConfigurationManager.AppSettings["Authority"], tenant);
    
            public void Configuration(IAppBuilder app)
            {
                app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    
                app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    AuthenticationType = "Cookies",
                    CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebCookieManager()
                });
    
                //app.UseCookieAuthentication(new CookieAuthenticationOptions
                //{
                //    //ExpireTimeSpan = t,
                //    Provider = new CookieAuthenticationProvider
                //    {
                //        OnResponseSignIn = context =>
                //        {
                //            context.Properties.AllowRefresh = true;
                //            context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(2);
                //        }
                //    }
                //});
                app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    // Sets the ClientId, authority, RedirectUri as obtained from web.config
                    ClientId = clientId,
                    Authority = authority,
                    RedirectUri = redirectUri,
                    // PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page
                    PostLogoutRedirectUri = redirectUri,
                    Scope = OpenIdConnectScope.OpenIdProfile,
                    // ResponseType is set to request the id_token - which contains basic information about the signed-in user
                    ResponseType = OpenIdConnectResponseType.IdToken,
                    // ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application
                    // To only allow users from a single organizations, set ValidateIssuer to true and 'tenant' setting in web.config to the tenant name
                    // To allow users from only a list of specific organizations, set ValidateIssuer to true and use ValidIssuers parameter 
                    TokenValidationParameters = new TokenValidationParameters()
                    {
                        ValidateIssuer = false
                    },
                    // OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method
                    Notifications = new OpenIdConnectAuthenticationNotifications
                    {
                        AuthenticationFailed = OnAuthenticationFailed
                    }
                }
            );
                //ConfigureAuth(app);
            }
    
            private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
            {
                context.HandleResponse();
                context.Response.Redirect("/?errormessage=" + context.Exception.Message);
                return Task.FromResult(0);
            }
        }
    }

By using this user login successfully to application but logout after 20 minutes.

My question is- How can I increase the logout time in Azure or Refresh the token?

Praveen Barawal
  • 191
  • 1
  • 1
  • 5

1 Answers1

1

You can configure token lifetime by following this document.

You can also refresh the access token using AcquireTokenByRefreshToken or acquire a new token silently using AcquireTokenSilent. The two answers in this post would be helpful.

Allen Wu
  • 15,529
  • 1
  • 9
  • 20