Running the C# QuickStart Sample
This topic explains how to install, run, and extend the QuickStart sample provided in the C# programming language.
Important: |
The C# QuickStart sample is preconfigured to use default values for the application ID, secret key, and return URL parameters. The return URL is preconfigured to point to http://localhost/webauth/sample/webauth-handler.aspx. For more information about how to customize these parameters, see Getting Your Application ID for Web Authentication. |
Installing the Sample
When you install the sample, you put its files in the correct locations on your Web server. In this discussion, we assume that you have direct access to your Web server and that you develop and browse your site from the server.
Before you install the C# QuickStart sample, make sure that Microsoft Internet Information Services (IIS), the .NET Framework 2.0, and ASP.NET are installed on your server. For IIS, make sure that:
- A default Web site exists
- The home directory of the default site is C:\Inetpub\wwwroot
To install the sample
- Run the C# sample installation file (webauth-cs-1.2.msi).
- Copy the C:\Program Files\Windows Live ID\WebAuth directory to C:\Inetpub\wwwroot.
- Create a directory named App_Code under C:\Inetpub\wwwroot, if it does not already exist.
- Move C:\Inetpub\wwwroot\WebAuth\App_Code\WindowsLiveLogin.cs to the default App_Code directory at C:\Inetpub\wwwroot\App_Code\.
The C# QuickStart sample is installed.
Running the Sample
After you install the sample, you can run it to see a demonstration of how Windows Live ID works.
To run the sample
- Go to http://localhost/webauth/sample/default.aspx.
- Click Sign in. You are directed to the Windows Live ID sign-in page.
- Type your valid Windows Live ID and password to sign in. You are redirected to the sample application. The Sign in link has now changed to Sign out.
- Click Sign out. You are signed out.
參考資料:http://msdn.microsoft.com/en-us/library/bb676630.aspx
首先,和Google AuthSub一樣,你必須先在Live的雲端運算開發中心Azure Services Developer Portal去註冊你的程式
註冊完成之後,就可以開始進行開發,需要說明的是,在Live ID網站上提供了一個非常簡單的類WindowsLiveLogin.cs,應該可以在這個例子之中得到這個類:Running the C# QuickStart Sample,而且可能是因為有了這個類,Microsoft覺得不需要再去提供什麼和服務端互動的文檔了(反正我沒有找到),因此,我就直接不改動任何程式的情況下使用了這個類,這樣,一切就方便了很多了。
先看如何得到用來讓使用者登入的轉向地址,這個太容易了,因為這正是WindowsLiveLogin類的方法,需要說明的是,WindowsLiveLogin類有幾個方法來獲得轉向地址的,其中GetLoginUrl方法得到的地址僅僅進行使用者登入,而不向使用者申請任何許可權,而GetConsentUrl(string scope)方法可以指定向使用者申請讀取相應資料的許可權,和Google AuthSub一樣,我們在使用Live的登入的時候也需要讀取使用者的地址本資訊來獲得使用者的登入帳號地址,因此,我們使用wll.GetConsentUrl("Contacts.View");方法來獲得轉向URL,("Contacts.View"代表對地址本的唯讀訪問),當然在使用這個方法之前,要先設定好PolicyUrl屬性(隱私申明地址)和ReturnUrl屬性(登入完成之後的迴轉地址);
有一點需要特別說明的是:在使用GetLoginUrl或者GetConsentUrl方法之前,Live要求使用者必須必須已經指定WindowsLiveLogin的PolicyUrl屬性,也就是隱私申明的地址,而且,要求指定的網址必須是能夠訪問的,似乎Live還會去檢查這個網址是否能夠訪問的,我在系統之中將隱私申明的網址指定為我的登入頁面,因為在那個頁面有我關於保護使用者隱私的申明。
將使用者轉向到剛才擷取的URL之後,使用者就會被轉向到Live ID的登入頁:
Live轉回到登入迴轉地址的時候,會有多種參數(delauth,login,logout,clearcookie),根據Live介面的要求,這些類型都應該實現,具體每個類型代表如下含義:
1.delauth,這個是我主要用到的類型,就是在使用者登入並授與存取權限之後,迴轉的類型,注意,必須調用GetConsentUrl方法才會得到這種迴轉,從參數之中可以擷取到ConsentToken參數,就是後面要用到的資料存取權杖。
2.login,這是調用GetLoginUrl方法之後迴轉的類型,也就是說,是不需要訪問使用者的任何資料的情況下迴轉訪問的參數類型;
3.logout,登出
4.clearcookie,清除Cookie