實現登入url跳轉

iDotNetSpace發表於2009-02-01

在開發及實際中 會是這樣,登入後自動返回請求的URL

由於所有頁面都繼承父頁面,可以在父頁面中得到url,登入以後轉向原請求頁面。

 

實現登入url跳轉
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt判斷是否登入的父類
using System;

namespace bchataspx.entity
{
    
    
/// 
    
/// AdminPager 的摘要說明。
    
/// 
    public class AdminPager : StockManagerPager
    {
        
protected User u = null;
        
public AdminPager()
        {
            
//
            
// TODO: 在此處新增建構函式邏輯
            
//
        }
        
protected override void OnLoad(EventArgs e)
        {
            
//http://127.0.0.1/bchataspx/manager/Sac/SacDataImport.aspx
           /*pointStart*/ 
            
string actionUrl = Request.RawUrl.ToString();//得到當前url
            actionUrl = Server.UrlEncode(Request.RawUrl.ToString());//對url進行編碼
            /*pointEnd*/ 
            
if(Request.Cookies["userId"]==null||Request.Cookies["userId"].Value.ToString().Trim()=="")
            {
                
if(actionUrl!="")                    
                    Response.Redirect(
"/bchataspx/admin/LoginMenu.aspx?ReturnUrl="+actionUrl);
                
else
                    Response.Redirect(
"/bchataspx/admin/LoginMenu.aspx");
            }
            
else
            {
                
int uid = int.Parse(Request.Cookies["userId"].Value.Trim());
                u 
= new User(uid);
                
base.OnLoad(e);
            }
        }

    }
}

 

登入按鈕事件也做相應調整

實現登入url跳轉
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt    private void btnSub_Click(object sender, System.EventArgs e)
        {
            
if(txtUserPwd.Text==""||txtUserName.Text=="")
            {
                Utils.MessageBox(
"使用者名稱或密碼不能為空!");
                
return;
            }
            
string  name = txtUserName.Text.Trim().Replace("'","");
            
string pwd = txtUserPwd.Text.Trim().Replace("'","");
        
            
string uid = ValidateLogin(name,pwd);
            
if(int.Parse(uid)>0)
            {    
                
//entity.User u = new User(uid);        
                HttpCookie cookie = new HttpCookie("userId",uid);//由於cookie只能保持string型別,uid也改成string型別
                Response.Cookies.Add(cookie);   

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-544946/,如需轉載,請註明出處,否則將追究法律責任。

相關文章