Это вспомогательный метод для проверки учетных данных из Active Directory.

акомился с некоторыми учебными пособиями по аутентификации веб-API с помощью OWIN. Большинство из этих руководств настраивают OAuthAuthorizationServerProvider. Тем не менее, когда я отлаживаю "F11" не достигают класса OAuthAuthorizationServerProvider

 private void ConfigureAuth(IAppBuilder app)
        {
            //
            app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {


            });

            //
            app.UseExternalSignInCookie(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);

            OAuthAuthorizationServerOptions authorizationServerOption = new OAuthAuthorizationServerOptions()
            {
                /*
                 * for demo only
                 * to enforce the Token retrieval over SSL (any non-https requests for requesting the Token will be denied)
                 * set AllowInsecureHttp = false
                */

               // AllowInsecureHttp = true,

                // Add token to the API dir
                //TokenEndpointPath = new PathString("/token"),

                //
                //Provider = new AWOAuthServerProvider(),

                // For test only 1 Day token expiry
                //AccessTokenExpireTimeSpan = TimeSpan.FromDays(1)

            };


            authorizationServerOption.AllowInsecureHttp         = true;
            authorizationServerOption.TokenEndpointPath         = new PathString("/token");
    /*break point*/
            authorizationServerOption.Provider                  = new AWOAuthServerProvider();
            authorizationServerOption.AccessTokenExpireTimeSpan = TimeSpan.FromDays(1);

            // Enable the application to use bearer tokens to authenticate users
            app.UseOAuthBearerTokens(authorizationServerOption);

            // Token Generation
            app.UseOAuthAuthorizationServer(authorizationServerOption);

            //Token Consumption
            app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
            {


            });

        }    

Как я могу использовать или вызвать метод в стороне класса OAuthAuthorizationServerProvider?

 public class AWOAuthServerProvider : OAuthAuthorizationServerProvider
    {
        public override async Task ValidateClientAuthentication
            (OAuthValidateClientAuthenticationContext context)
        {
            await Task.FromResult(context.Validated());
        }

        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext  context)
        {

            if (!ValidCredential(context.Password,context.UserName))
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }


            var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
            identity.AddClaim(new Claim("username", context.UserName));

         context.Validated(identity);

        }

Это вспомогательный метод для проверки учетных данных из Active Directory.

         private bool ValidCredential (String password,String username)
                {
                    string[] NTId           = { "", "" };
                    string   netDomain      = "";
                    string   netUserName    = "";
                    bool     isValid        = false;

                    //
                    // context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });


                    /*****************************************************************************************/
                    if (username.Equals(null) || username.Equals(""))
                    {
                        //Request client Network username
                        try
                        {
                            NTId = (HttpContext.Current.Request.LogonUserIdentity.Name)
                                                   .Replace(@"\\", @"\")
                                                   .Split('\\');
                        }
                        // error
                        catch (Exception e)
                        {
                            return false;
                        }
        }
 if (NTId.Length == 2)
                {
                    netDomain = NTId[0];
                    netUserName = NTId[1];
                }
     try
                {
                    using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, netDomain))
                    {

                        isValid = principalContext.ValidateCredentials(netUserName, password);
                    }
                }
                // error 
                catch (Exception e)
                {
                    return false;
                }


                return isValid;
    }

Thinks

Ответы на вопрос(0)

Ваш ответ на вопрос