这里介绍如何使用Windows LiveID在ASP.NET应用程序中验证用户的身份,说穿了这也是一种特殊验证模型,也就是说当在您的网站中使用Windows LiveID登录时,此用户首先会重定向到Windows Live登录页,然后通过验证的用户WindowsLive将该用户再重定向到您的网站,并提供一个用户ID。
使用Windows Live ID
要开始使用的Windows Live ID ,首先你需要申请一个应用程序ID。转到:https://msm.live.com/app/default.aspx 注册应用程序ID,其实微软针对此应用还有一个指南--->http://msdn.microsoft.com/en-us/library/cc287659.aspx
上面这个注册表格要填的我都填了,其中要注意的是 返回的URL,这个页面主要是处理与 Windows Live ID的沟通,如我现在VS的启动页是:
http://localhost:15650/WebAuth/Sample/webauth-handler.aspx
下载并安装Windows Live ID Web身份验证的SDK
呵呵,老样子,微软已经为ASP.NET开发者已经提供了非常容易实现的示例模板。这里下载:http://www.microsoft.com/downloads/details.aspx?FamilyId=E565FC92-D5F6-4F5F-8713-4DD1C90DE19F&displaylang=en, 准确点可以看该文:http://livesino.net/archives/302.live。默认安装路径在:C:\Program Files\Windows Live ID\WebAuth。
整合到您的网站
首先用VisualStudio2008或者2005创建一个Asp.Net网站,然后将安装的示例模板中的App_Code文件夹中的 WindowsLiveLogin.cs(C:\Program Files\Windows LiveID\WebAuth\Sample\App_Code\WindowsLiveLogin.cs)添加到我们的App_Code文件夹中。这里 我直接用微软的示例,不自己新建网站了。
然后在web.config文件中配置刚申请一个应用程序ID 000000004400D659,
Default.aspx页面中的HTML的代码如下:[code] <body>
<table width="320">
<tr>
<td>
<h1>
欢迎来到Windows Live ID™网络身份验证SDK中的C#模板</h1>
<p>
下面的链接表明您是否登录。如果该链接的文本是 <b>Sign in</b>, 表明您尚未登录。如果它显示 <b>Sign out</b>,表明您已经登录。</p>
<iframe id="WebAuthControl" name="WebAuthControl"
src="http://login.live.com/controls/WebAuth.htm?appid=<%=AppId%>&style=font-size%3A+10pt%3B+font-family%3A+verdana%3B+background%3A+white%3B"
width="80px" height="20px" marginwidth="0" marginheight="0" align="middle" frameborder="0"
scrolling="no"></iframe>
<p>
<% if ( UserId == null ) { %>
你还没有登录到网站,请点击 <b>Sign in</b> 链接.
<% }
else { %>
您已经登录到网站,您的用户编号ID = "<b><%=UserId%></b>".
<% } %>
</p>
</td>
</tr>
</table>
</body>
</html> [/code]Default.aspx文件的代码隐藏文件代码如下:[code]using System;
using System.Web;
using System.IO;
using WindowsLive;
/// <summary>
/// This is the default aspx.cs page for the sample Web Auth site.
/// It gets the application ID and user ID for display on the main
/// page.
/// </summary>
public partial class DefaultPage : System.Web.UI.Page
{
const string LoginCookie = "webauthtoken";
// Initialize the WindowsLiveLogin module.
static WindowsLiveLogin wll = new WindowsLiveLogin(true);
protected static string AppId = wll.AppId;
protected string UserId;
protected void Page_Load(object sender, EventArgs e)
{
/* If the user token has been cached in a site cookie, attempt
to process it and extract the user ID. */
HttpRequest req = HttpContext.Current.Request;
HttpCookie loginCookie = req.Cookies[LoginCookie];
if(loginCookie != null){
string token = loginCookie.Value;
if (!string.IsNullOrEmpty(token))
{
WindowsLiveLogin.User user = wll.ProcessToken(token);
if (user != null)
{
UserId = user.Id;
}
}
}
}
}[/code]
Default.aspx将作为网站的一个登录页。 用户点击登录按钮,将被重定向到的Windows Live 登录页面。当他微软进行身份验证后,微软会重新回到我们的处理程序页上,所以,我们现在将创建一个新的aspx页,并将其命名webauth- handler.aspx。 还记得,当我们在注册申请Windows Live 时提供的返回URL的网页吗。
webauth- handler.aspx页面将作为处理Windows Live重定向回到您的网站用户的网页。 而且微软WindowsLive将提供一个验证特定值给您使用。 我们可以简单地删除所有的标记,下面是webauth-handler.aspx代码隐藏类的代码:
using System;
using System.Web;
using System.IO;
using WindowsLive;
/// <summary>
/// This page handles the login, logout and clearcookie Web Auth
/// actions. When you create a Windows Live application, you must
/// specify the URL of this handler page.
/// </summary>
public partial class HandlerPage : System.Web.UI.Page
{
const string LoginPage = "default.aspx";
const string LogoutPage = LoginPage;
const string LoginCookie = "webauthtoken";
static DateTime ExpireCookie = DateTime.Now.AddYears(-10);
static DateTime PersistCookie = DateTime.Now.AddYears(10);
// Initialize the WindowsLiveLogin module.
static WindowsLiveLogin wll = new WindowsLiveLogin(true);
protected void Page_Load(object sender, EventArgs e)
{
HttpRequest req = HttpContext.Current.Request;
HttpResponse res = HttpContext.Current.Response;
// Extract the 'action' parameter from the request, if any.
string action = req["action"];
/*
If action is 'logout', clear the login cookie and redirect
to the logout page.
If action is 'clearcookie', clear the login cookie and
return a GIF as response to signify success.
By default, try to process a login. If login was
successful, cache the user token in a cookie and redirect
to the site's main page. If login failed, clear the cookie
and redirect to the main page.
*/
if (action == "logout")
{
HttpCookie loginCookie = new HttpCookie(LoginCookie);
loginCookie.Expires = ExpireCookie;
res.Cookies.Add(loginCookie);
res.Redirect(LogoutPage);
res.End();
}
else if (action == "clearcookie")
{
HttpCookie loginCookie = new HttpCookie(LoginCookie);
loginCookie.Expires = ExpireCookie;
res.Cookies.Add(loginCookie);
string type;
byte[] content;
wll.GetClearCookieResponse(out type, out content);
res.ContentType = type;
res.OutputStream.Write(content, 0, content.Length);
res.End();
}
else
{
WindowsLiveLogin.User user = wll.ProcessLogin(req.Form);
HttpCookie loginCookie = new HttpCookie(LoginCookie);
if (user != null)
{
loginCookie.Value = user.Token;
if (user.UsePersistentCookie)
{
loginCookie.Expires = PersistCookie;
}
}
else
{
loginCookie.Expires = ExpireCookie;
}
res.Cookies.Add(loginCookie);
res.Redirect(LoginPage);
res.End();
}
}
}
第一次登录测试 当以上操作都完成后,现在应该可以使用Windows Live ID登录了,我们首页浏览登录页面Default.Aspx:
点Sign In链接后,将重定向到Windows Live的登录页面:
当Windows Live ID验证通过,Widnows Live将返回到我们的登录页面,下面这个页面现在将显示欢迎您的信息和您的用户ID :
