ASP.NET Forms Authentication without Redirect

2019-07-11 21:48发布

I am using ASP.Net's forms authentication, but do not want the default behavior of redirecting to a login page when a restricted area is accessed. Instead I would like to invoke a javascript JQuery dialog for the login on the current page, preventing the content behind from loading.

My only issue is that by default the forms authentication wants to redirect.

Is there a handler that I can hook into, or some other option to prevent the redirect?

3条回答
小情绪 Triste *
2楼-- · 2019-07-11 21:56

Yes, if you deal with JQuery review possibility to use standard ASP.Net ajax oriented Sys.Services.AuthenticationService.

查看更多
再贱就再见
3楼-- · 2019-07-11 21:56

You might want to look into the LoginView control. This lets you set different templates for anonymous users and users that have logged in. It still uses ASP.NET authentication, so you can keep your current forms based authentication.

查看更多
甜甜的少女心
4楼-- · 2019-07-11 22:00

In addition to Dewfy:

Add the following to your web.config:

<system.web.extensions>
    <scripting>
      <webServices>
        <authenticationService enabled="true" />    
      </webServices>
    </scripting>
  </system.web.extensions>

You can now call the authentication service from JavaScript like:

 Sys.Services.AuthenticationService.login("username",
    "password", false, null, null, loginCompleted, loginFailed, "");

 function loginCompleted()
 {
     var loginSuccessful = Sys.Services.AuthenticationService.get_isLoggedIn();
     if(loginSuccessful) /* do stuff */
 }

 function loginFailed(result, userContext, method)
 {
     alert('Exception ' + result.get_message());
 }
查看更多
登录 后发表回答