Forms Authentication.Set AuthCookie&Forms Authentication.RedirectFroom Loginge

10621 ワード

まずForms Authentication.Set AuthCookieの解釈を見てみます。
参照:http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.setauthcookie.aspx
Creates an authentication ticket for the supplied user name and adds it to the cookies collection of the ress ponse,or to the URL if you are cookieless authentication.
提供されたユーザ名のために認証証を作成し、それを追加します。 レスポンス要求の cookies collectionには(Cookieが足りないので応答要求のURLに追加する)
私は英語が下手です。通訳はみんなを笑わせました。
Name
Description
Set AuthCookie(String,Boolean)
Creates an authentication ticket for the supplied user name and adds it to the cookies collection of the ress ponse,or to the URL if you are cookieless authentication.
Set AuthCookie(String,Boolean,String)
Creates an authentication ticket for the supplied user name and adds it to the cookies collection of the ress ponse,using the supplied cookie path,or using the URL if you cookie authentication.
この中の通訳は言いません。これは自然に何を説明しているのか分かります。このようなものを説明するのは難しいです。
詳しく説明してください。上記の参照urlを見てください。ここでは使い方だけを説明します。
 
 またForms Authentication.RedirectFrogingeの説明を見にきます。
参照:http://msdn.microsoft.com/zh-cn/library/system.web.security.formsauthentication.redirectfromloginpage(v=VS.80).aspx
認証されたユーザは、最初に要求されたURLまたはデフォルトURLにリダイレクトする。
名前
説明
Forms Authentication.RedirectFroom LogingPage(String,Boolean)
認証されたユーザは、最初に要求されたURLまたはデフォルトURLにリダイレクトする。
Forms Authentication.RedirectFroom LogingPage(String,Boolean,String)
Formsアイデンティティを使用してCookieの指定Cookieパスを検証し、アイデンティティ認証を経たユーザは、最初に要求されたURLまたはデフォルトURLにリダイレクトする。
 説明は言わないで、下の直接的な例をあげて、私も読者が煩わすことを恐れて、要るのは既製のコードではありませんか?ガガ、実は私もそう思います。
 
1.自身の登録コントロールで認証ログインしていないと、ページCookieに値がないので、ページの検証が必要になります。ユーザログイン検証に成功した後、ユーザのログインID値をAuthCookieに渡す必要があります。以下はログイン認証で、ログインユーザ情報をAuthCookieに追加します。
キーコード:
Forms Authentication.Set AuthCookie(user Name,false);Forms Authentication.RedirectFroom LogingPage;
 
     protected void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.Trim();
            string password = txtPassword.Text.Trim();
            string validateCode = txtValidateCode.Text.Trim();

            BizResult<bool> bizResult = UserManager.UserLogin(userName, password, validateCode);

            if (bizResult != null && bizResult.IsSuccess)
            {            
                FormsAuthentication.SetAuthCookie(userName, false);
                FormsAuthentication.RedirectFromLoginPage(userName, false);
                
                string message = "      ";
                //#if !DEBUG
                BaseConfigManager baseManager = new BaseConfigManager();
                baseManager.AddBillingOperationLog(3, message,  1);
                //#endif
            }
            else
            {
                ClientScript.RegisterStartupScript(typeof(_Default), "NO_SUCH_EMPLOYEE", "<script>alert('" + bizResult.ReturnString + "')</script>");
            }
        }
 
 2.現在のユーザーを獲得する(これは使う鍵です。AuthCookieに物を入れて、どうやって使うか分かりません。腫れた莫はどうすればいいですか?)
キーコード:
HttpCookie cookie=HttpContactext.Current.Request.Coookis.Get(Forms Authentication.Forms Cookie Name)Forms Authentication Ticket ticket=Forms Authentication.Decrypt;string userName=tickett.Name;
 
 
/// <summary>
///       
/// </summary>
/// <returns></returns>
public static UserInfo GetCurrentUser()
{
    HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);

    if (cookie == null || string.IsNullOrEmpty(cookie.Value))
        return null;

    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
    string userName = ticket.Name;

    if (!string.IsNullOrEmpty(userName))
    {
        if (userName == CmfuConfig.Instance.AppSetting.PowerUserName)
        {
            UserInfo user = new UserInfo();
            user.Id = -1;
            user.UserName = CmfuConfig.Instance.AppSetting.PowerUserName;
            user.PassWord = CmfuConfig.Instance.AppSetting.PowerUserPwd;
            user.PageRoleId = CmfuConfig.Instance.AppSetting.ADMINPAGEROLE;
            user.AppRoleId = CmfuConfig.Instance.AppSetting.ADMINAPPROLE;
            user.AppRoleName = "     ";
            user.PageRoleName = "     ";
            user.Status = 1;

            return user;
        }
        else
        {
            BizResult<UserInfo> returnObj = UserManager.GetUserByUserName(userName);
            if (returnObj != null && returnObj.IsSuccess)
                return returnObj.ReturnObject;
        }
    }

    return null;
}
 
3.現在ログインしているユーザが終了します。
protected void lbtnSignOut_Click(object sender, EventArgs e)
{
    FormsAuthentication.SignOut();//
   Response.Redirect("/Login.aspx");//     
}
 
まとめ:
Forms Authentication.Set AuthCookieはアイデンティティ検証票を作成し、Cookie Forms Authenticationに追加しました。SetAuthCookieとRedirectFrogingeの2つの方法があります。ユーザーの登録情報(マーク)をCookieに記録し、Forms認証方式のHttpContect.Current.Uted.Inticed.Inticed。このCookieの情報により、ユーザがログインしているかどうかを判断します。Forms Authentication.SignOutはこのCookieマークをクリアするために使用されます。