コード例 #1
0
        public void ConfigureAuth(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
            app.UseCookieAuthentication(new CookieAuthenticationOptions());

            app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions {
                ClientId      = SettingsHelper.ClientId,
                Authority     = SettingsHelper.AzureADAuthority,
                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    AuthorizationCodeReceived = (context) => {
                        string code = context.Code;

                        ClientCredential creds = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret);
                        string userObjectId    = context.AuthenticationTicket.Identity.FindFirst(System.IdentityModel.Claims.ClaimTypes.NameIdentifier).Value;

                        EFADALTokenCache cache            = new EFADALTokenCache(userObjectId);
                        AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.AzureADAuthority, cache);

                        Uri redirectUri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path));
                        AuthenticationResult authResult = authContext.AcquireTokenByAuthorizationCode(code, redirectUri, creds, SettingsHelper.AzureAdGraphResourceId);

                        return(Task.FromResult(0));
                    },
                    AuthenticationFailed = (context) => {
                        context.HandleResponse();
                        return(Task.FromResult(0));
                    }
                },
                TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters {
                    ValidateIssuer = false
                }
            });
        }
コード例 #2
0
        public static AuthenticationContext GetAuthContext()
        {
            var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
            //var tokenCache = new NaiveSessionCache(signInUserId);
            var tokenCache  = new EFADALTokenCache(signInUserId);
            var authContext = new AuthenticationContext(SettingsHelper.AzureADAuthority, tokenCache);

            return(authContext);
        }
コード例 #3
0
        public void ConfigureAuth(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
            app.UseCookieAuthentication(new CookieAuthenticationOptions());

            //configure the OWIN OpenID Connect options
            app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                ClientId      = SettingsHelper.ClientId,
                Authority     = SettingsHelper.AzureADAuthority,
                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    // when an auth code is received
                    AuthorizationCodeReceived = (context) =>
                    {
                        // get the openid connect code passed from azure ad on a successful auth
                        string code = context.Code;

                        // create app cred & get the user
                        ClientCredential creds = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret);
                        string userObjectId    =
                            context.AuthenticationTicket.Identity.FindFirst(System.IdentityModel.Claims.ClaimTypes.NameIdentifier)
                            .Value;

                        // get access & refresh token
                        EFADALTokenCache cache            = new EFADALTokenCache(userObjectId);
                        AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.AzureADAuthority,
                                                                                      cache);

                        // obtain access token for the azure ad graph
                        Uri redirectUri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path));
                        AuthenticationResult authResult = authContext.AcquireTokenByAuthorizationCode(
                            code, redirectUri, creds, SettingsHelper.AzureAdGraphResourceId);

                        // success auth
                        return(Task.FromResult(0));
                    },
                    AuthenticationFailed = (context) =>
                    {
                        context.HandleResponse();
                        return(Task.FromResult(0));
                    }
                },
                TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer = false
                }
            });
        }
コード例 #4
0
        public void ConfigureAuth(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
            app.UseCookieAuthentication(new CookieAuthenticationOptions());
            app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                ClientId      = SettingsHelper.ClientId,
                Authority     = SettingsHelper.AzureADAuthority,
                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    AuthorizationCodeReceived = (context) =>
                    {
                        var code         = context.Code;
                        var creds        = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret);
                        var userObjectId = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
                        //var tokenCache = new NaiveSessionCache(userObjectId);
                        var tokenCache = new EFADALTokenCache(userObjectId);

                        var authContext = new AuthenticationContext(SettingsHelper.AzureADAuthority, tokenCache);
                        var redirectUri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path));
                        var authResult  = authContext.AcquireTokenByAuthorizationCode(
                            code, redirectUri, creds, SettingsHelper.GraphResourceId);
                        return(Task.FromResult(0));
                    },
                    AuthenticationFailed = (context) =>
                    {
                        context.HandleResponse();
                        return(Task.FromResult(0));
                    },
                    RedirectToIdentityProvider = (context) =>
                    {
                        // This ensures that the address used for sign in and sign out is picked up dynamically from the request
                        // this allows you to deploy your app (to Azure Web Sites, for example) without having to change settings
                        // Remember that the base URL of the address used here must be provisioned in Azure AD beforehand.
                        string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase;
                        context.ProtocolMessage.RedirectUri           = appBaseUrl + "/";
                        context.ProtocolMessage.PostLogoutRedirectUri = appBaseUrl;

                        return(Task.FromResult(0));
                    }
                },
                TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                {
                    ValidateIssuer = false
                }
            });
        }