0

これをasp.netに使用するJqueryログイン制御のようなコードがありますが、ユーザー名とパスワードを簡単に認証できる場所と方法がわかりません。

Login.aspx

<head>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/login.js" type="text/javascript"></script>
</head>

 <div id="loginBox">                
                    <form id="loginForm">
                        <fieldset id="body">
                            <fieldset>
                                <label for="email">Email Address</label>
                                <input type="text" name="email" id="email" />
                            </fieldset>
                            <fieldset>
                                <label for="password">Password</label>
                                <input type="password" name="password" id="password" />
                            </fieldset>
                            <input type="submit" id="login" value="Sign in" />
                            <label for="checkbox"><input type="checkbox" id="checkbox" />Remember me</label>
                        </fieldset>
                        <span><a href="#">Forgot your password?</a></span>
                    </form>
                </div>

Login.js
// Login Form

$(function() {
    var button = $('#loginButton');
    var box = $('#loginBox');
    var form = $('#loginForm');
    button.removeAttr('href');
    button.mouseup(function(login) {
        box.toggle();
        button.toggleClass('active');
    });
    form.mouseup(function() { 
        return false;
    });
    $(this).mouseup(function(login) {
        if(!($(login.target).parent('#loginButton').length > 0)) {
            button.removeClass('active');
            box.hide();
        }
    });
});

このJqueryコードでasp.net認証を使用するにはどうすればよいですか? c#認証コードをどこでどのように書くのですか? いくつかのスレッドも与えてください m new bie in jquery ありがとう

4

2 に答える 2

3

まず、ajax呼び出しを作成する方法は次のとおりです。

var usernameText = $("#usernameTextbox");
var passwordText = $("#passwordTextbox");

$.ajax({
    type: 'GET',
    url: '/Account/ValidateUser',
    async: true,
    dataType: 'json',
    data:
    {
        userName: usernameText,
        password: passwordText
    },
    success: function(result) {
        if(result){
            // redirect user to required page
        }
        else{
            // show error on some div that usrname and password do not match
        }
    }
});

SOに関する関連質問

MVC 4JqueryAjax呼び出しはログインページhtmlを返します

jQueryとAJAXで基本認証を使用する方法は?

于 2012-10-29T07:22:05.210 に答える
1

正確に何をしようとしているのかはわかりませんが、asp.net でログオンするには、何らかの方法でデータをサーバーに送信する必要があります。フォームで送信するか、AJAX リクエストで送信するという 2 つの方法があります。フォーム内で送信したい場合は、タグフォームでアクションを指定する必要があります。

<form id="loginForm" action="MySite/Logon">
    <!-- fields, labels, etc. -->
</form>

jQuery AJAX リクエスト経由で送信する場合は、ここjQuery.ajaxで明確に説明されているメソッドを、対応する URL をパラメータとして使用できます。 サーバー側では、認証は次のようになります。url

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( // create authentication ticket
    1, // id
    email, // user name
    DateTime.Now, // date of issue
    DateTime.Now.AddMinutes(15), // expiration date
    true, // true if your ticket will be stored in a cookie, that will be kept by browser across the sessions
    userdata, // any text data, that you want to add to ticket (e.g. user roles)
    FormsAuthentication.FormsCookiePath // path associated with your ticket
);
string hashedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
    FormsAuthentication.FormsCookieName, hashedTicket); // adding your ticket to cookie
cookie.Expires = ticket.Expiration;
Response.Cookies.Add(cookie); // sending cookie to client with current response

これらのアクションの後、クライアントがログオフするか、Cookie/チケットの有効期限が切れるまで、クライアントは認証済みとして認識されます。
この情報がお役に立てば幸いです。

于 2012-10-29T07:35:27.557 に答える