如何在ASP.NET中使用Windows Live Web Bar
前言
Messenger Platform 團隊將在 Mix 09 上推出新的 Windows Live Messenger Web Toolkit 和 Web Bar 控件,其中Web Bar控件包含完整的 Messenger 體驗,功能包括:
??? 聯(lián)系人列表 — 管理、搜索、發(fā)起對話,還支持 Favorites 聯(lián)系人;
??? 對話列表 — 用戶切換、管理活動對話;
??? 用戶區(qū) — 顯示用戶狀態(tài),并允許用戶更新信息,;
??? 跨頁導航 — 能在站內切換頁面的同時,保持 Windows Live Messenger 的登錄狀態(tài)。
?? Web Bar Control 將會顯示在網站的底部,從各個方面來看很類似 Facebook Chat 功能。
若網站已經整合 Web Bar 控件,訪客即可通過 Windows Live Messenger 用戶熟悉的界面發(fā)起對話。同時,所有訪問該站點的訪客都可通過該控件進行交互,甚至不必加入對方好友列表就可聊天,還能通過客戶端。
網站管理員可以將 Web Bar 作為獨立的控件使用,也可對 Web Toolkit 擴展開發(fā),當然,也可以通過 CSS 定制自己的應用樣式,以及提供大量的設置項。目前為止,微軟官方已經大量使用了 Messenger Library,就已推出的 Windows Live Messenger Web Toolkit 和 Web Bar 意味著什么?就目前的信息來看,這兩款新控件所做的就是幫助站長增強網站的交互性,提高網站黏度,畢竟即時聊天的優(yōu)勢還是不容輕視的。
本文將介紹如何容易的將Web Bar整合到自己的網站中。
?
首先,下載Windows Live? Messenger Web Toolkit SDK
可以在這個網站中下載應用程序的所有示例代碼:http://messenger.mslivelabs.com/。
只需下載sample code即可:http://messenger.mslivelabs.com/Samples.zip。
?
其次,在Windows Azure development portal中獲取application ID 和 secret Key
?
??? 首先,使用Windows Live帳號登錄到Azure,
??????
??? 當你登錄到Azure后,你需要新建一個項目,在IE 左上角可以看到一個“新建項目的(New Project )”超鏈接。
??????
??? 點擊鏈接進去后,你可以看到4種不同類型的項目,這里我們選擇最下面的“Live Services: Existing APIs”申請注冊。
??????
??? 在注冊表單中請?zhí)顚戫椖棵Q,以及在網絡有效的域名,請注意,這里的填寫的域名必須在網絡上真實存在。返回的地址是登錄成功后Windows Live返回的地址,。
填寫完后,點擊創(chuàng)建按鈕。如果創(chuàng)建成功,我們將得到Azure提供的application ID 和 secret Key。
?
?
最后,整合到您的ASP.NET網站
首先用Visual Studio2008或者2005創(chuàng)建一個Asp.Net網站,如果你沒有下載Windows Live? Messenger Web Toolkit SDK的示例,那么你需要有一個Privacy.htm頁面和一個Channel.htm頁面,這里我們可以按照我們下載好的Windows Live? Messenger Web Toolkit SDK教程中完全相同的代碼來做,教程的示例在?:\\?\Samples\WebToolkit\GettingStarted\CSharp里面。
第一步是打開Web.config文件,在AppSettings節(jié)中配置我們獲取的application ID( 應用程序ID) 和 secret Key(密鑰 )。
再打開Default.aspx,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DefaultPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:msgr="http://messenger.live.com/2009/ui-tags"> <head><title>Get Started with the Windows Live Messenger Web Toolkit</title><script type="text/javascript" src="http://www.wlmessenger.net/api/3.0/loader.js"></script><script type="text/javascript">Microsoft.Live.Core.Loader.load(['Messenger.UI', 'Messenger.UI.Styles.Core'], null);</script><style type="text/css">html, body{font-family: Verdana, Arial, Tahoma;margin: 0px; } .content { margin-left: 10px;}</style> </head> <body><!-- Initialize the application --><msgr:app id="appTag" application-verifier-token="<%= ApplicationVerifier %>" privacy-url="Privacy.html"channel-url="Channel.html"token-url="RefreshMessengerToken.aspx"></msgr:app> </body> </html> 在上述HTML代碼中,我們引用了最新的Windows Live 3.0的API ,并使用Load方法加載初始化, <script type="text/javascript" src="http://www.wlmessenger.net/api/3.0/loader.js"></script><script type="text/javascript">Microsoft.Live.Core.Loader.load(['Messenger.UI', 'Messenger.UI.Styles.Core'], null);</script> 其次,在頁面中還增加了一個XML架構鏈接, <html xmlns="http://www.w3.org/1999/xhtml" xmlns:msgr="http://messenger.live.com/2009/ui-tags">這個架構可以使我們在頁面中使用<msgr:>標簽來顯示即時通訊的用戶界面,比如Web Messenger的<msgr:app>標記。
<msgr:app id="appTag" application-verifier-token="<%= ApplicationVerifier %>" privacy-url="Privacy.html"channel-url="Channel.html"token-url="RefreshMessengerToken.aspx"></msgr:app>在<msgr:app>的鏈接中,我們可以看到privacy-url屬性鏈接到頻道頁面, channel-url屬性鏈接到隱私頁面,而token-url鏈接到應用程序的驗證頁面(RefreshMessengerToken.aspx),
RefreshMessengerToken.aspx頁面將通過Windows Live服務驗證您的Windows Azure development portal申請是否有效以及從本機刪除COOKIE,以及登出等所有事情,具體代碼如下:
1: <%@ Page Language="C#" %> 2: <%@ Import Namespace="WindowsLive" %> 3:? 4: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 5:? 6: <!-- 7: Copyright (c) Microsoft Corporation. All rights reserved. 8: --> 9:? 10: <script runat="server"> 11: /* 12: * Configuration: 13: * The minimum requirement is to specify the application id and application secret in web.config 14: * Refer to the boolean constructor of the WindowsLiveLogin for additional configuration options. 15: */ 16:? 17: /// <summary> 18: /// The name of the consent token cookie 19: /// </summary> 20: private const string ConsentTokenCookieName = "msgr-consent-token"; 21:? 22: /// <summary> 23: /// The name of the delegation token cookie 24: /// </summary> 25: private const string DelegationTokenCookieName = "msgr-delegation-token"; 26:? 27: /// <summary> 28: /// Current consent token that is stored in the conset cookie 29: /// </summary> 30: private string consentToken = null; 31:? 32: /// <summary> 33: /// Provides token processing functionality 34: /// </summary> 35: private WindowsLiveLogin windowsLiveLogin; 36:? 37: /// <summary> 38: /// Occurs when the page loads 39: /// </summary> 40: /// <param name="sender"></param> 41: /// <param name="e"></param> 42: 43: private void Page_Load(object sender, EventArgs e) 44: { 45: try 46: { 47: if (IsConsentCookieAvailable()) 48: { 49: RefreshDelegatedAuthenticationCookie(); 50: } 51: } 52: catch (Exception ex) 53: { 54: HandleException(ex); 55: } 56: } 57:? 58: /// <summary> 59: /// Tests whether the consent token cookie is available 60: /// </summary> 61: /// <returns>'true' if the consent token cookie is available</returns> 62: private bool IsConsentCookieAvailable() 63: { 64: HttpCookie consentTokenCookie = Request.Cookies[ConsentTokenCookieName]; 65: 66: if (consentTokenCookie == null) 67: { 68: return false; 69: } 70:? 71: this.consentToken = consentTokenCookie.Value; 72:? 73: return !string.IsNullOrEmpty(this.consentToken); 74: } 75:? 76: /// <summary> 77: /// Refreshes the token 78: /// </summary> 79: private void RefreshDelegatedAuthenticationCookie() 80: { 81: this.windowsLiveLogin = new WindowsLiveLogin(true); 82: WindowsLiveLogin.ConsentToken token = 83: this.windowsLiveLogin.ProcessConsentToken(this.consentToken); 84: bool expireDelegationToken = false; 85:? 86: if (token == null) 87: { 88: expireDelegationToken = true; 89: } 90: else 91: { 92: if (!token.IsValid()) 93: { 94: if (token.Refresh()) 95: { 96: UpdateDelegatedAuthenticationCookie(token); 97: } 98: else 99: { 100: expireDelegationToken = true; 101: } 102: } 103: else if (!IsDelegationTokenCookiePresent()) 104: { 105: UpdateDelegatedAuthenticationCookie(token); 106: } 107: } 108:? 109: if (expireDelegationToken) 110: { 111: ExpireDelegatedAuthenticatonCookie(); 112: } 113: } 114: 115: /// <summary> 116: /// Updates the cookies with a refreshed token 117: /// </summary> 118: /// <param name="token">The refreshed token</param> 119: private void UpdateDelegatedAuthenticationCookie(WindowsLiveLogin.ConsentToken token) 120: { 121: HttpCookie delAuthCookie = new HttpCookie(DelegationTokenCookieName); 122: delAuthCookie.Value = token.DelegationToken; 123: delAuthCookie.Path = "/"; 124: Response.Cookies.Add(delAuthCookie); 125:? 126: HttpCookie consentCookie = new HttpCookie(ConsentTokenCookieName); 127: consentCookie.Value = token.Token; 128: consentCookie.Path = "/"; 129: Response.Cookies.Add(consentCookie); 130: } 131: 132: /// <summary> 133: /// Expires the delegation token cookie 134: /// </summary> 135: private void ExpireDelegatedAuthenticatonCookie() 136: { 137: ExpireCookie(DelegationTokenCookieName); 138: } 139: 140: /// <summary> 141: /// Expires a given cookie 142: /// </summary> 143: /// <param name="cookieName">The name of the cookie to expire</param> 144: private void ExpireCookie(string cookieName) 145: { 146: HttpCookie cookie = new HttpCookie(cookieName); 147: cookie.Expires = DateTime.Now.AddYears(-1); 148: cookie.Path = "/"; 149: Response.Cookies.Add(cookie); 150: } 151:? 152: /// <summary> 153: /// Checks if the delegation token cookie is present 154: /// </summary> 155: /// <returns>'true' is returned if the delegation cookie is present</returns> 156: private bool IsDelegationTokenCookiePresent() 157: { 158: HttpCookie cookie = Request.Cookies[DelegationTokenCookieName]; 159: return cookie != null && !string.IsNullOrEmpty(cookie.Value); 160: } 161: 162: private void HandleException(Exception e) 163: { 164: // The application should log the exception as it may be caused due to a configuration issue 165: } 166: 167: </script> 168:? 169: <html xmlns="http://www.w3.org/1999/xhtml"> 170: <head runat="server"> 171: <title></title> 172: </head> 173: <body> 174: <form id="form1" runat="server"> 175: <div> 176: 177: </div> 178: </form> 179: </body> 180: </html>在Default.aspx中的最下面,還有一個<msgr:bar></msgr:bar>標記,這個標記將在我們的網頁底部顯示即時通訊的Messenger Web Bar。。最后,在Default.aspx的代碼隱藏文件有一個服務端的變量,它將取出Web.config中的數據。此外,在App_Code文件夾中的WindowsLiveLogin.cs是網站的登錄助手。
?
第一次登陸測試
第一次打開網站,我們可以看到在網頁的下邊
點擊登錄鏈接( “Sign in” )
?
?
完畢!
轉載于:https://www.cnblogs.com/jaic-xiao/archive/2009/04/30/Ru_He_Zai_ASPNET_Zhong_Shi_Yong_Windows_Live_Web_Bar.html
總結
以上是生活随笔為你收集整理的如何在ASP.NET中使用Windows Live Web Bar的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈Sharepoint权限
- 下一篇: SQL 胡言乱语