我正在做一个需要网站和移动应用程序的项目,几乎具有相同的功能。因此,为此我正在使用Asp.net Web API构建 API ,它也将用于网站和移动应用程序。
API地址是:api.domain.com 网站地址是:testing.domain.com
问题:
现在在 api.domain.com,我允许用户登录,FormsAuthentication在那里与Cookies一起使用。
当用户通过身份验证时,它会获得200 个 Http Satus以及Set-Cookie Header,其中还提到了允许的域。
这是请求和响应:
**Request**
Request URL:http://api.domain.com/account/login
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:139
Content-Type:application/x-www-form-urlencoded
Host:api.domain.com
Origin:http://testing.domain.com
Referer:http://testing.domain.com/Login.aspx?ReturnUrl=%2fteacherpanel
User-Agent:Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
Form Dataview URL encoded
__VIEWSTATE:/wEPDwUKMTY1NDU2MTA1MmRkmpJ4YQ4kteUpklwLOmcHppPgq/RHqfw7tilyk8pJp0Y=
UserName:ishan
Password:pass
**Response**
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Content-Length:0
Date:Sat, 22 Sep 2012 10:23:38 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/7.5
Set-Cookie:.Teacher=A3BD94D26CF733F6F223198ADE40D87C76D8ECC663D7CEDD6E3FF18B0ED23032F6089EF24141E0B65F3F29503A3AC1670C92B9CE4EF7D986974ABE61AB5F0C837245D1A30A8D8E8E058F9AFDD89281CBAB9A3EB98B4A320E689718AF9E76E4911506EBA7FD4244336D8409CFB6D77B179764726B550AB0FFF7A6508658615A57; domain=domain.com, .domain.com; path=/; HttpOnly
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
现在根据响应,浏览器应该将此 cookie 发送到服务器,并将每个请求发送到此域domain.com或其任何子域。
但这并没有发生,今天早上它对我有用,但我不知道发布后发生了什么。
另外为了更多参考,这里是我用来设置 cookie 并检查用户是否登录的代码。
此处为登录用户,此检查在 API 中执行,位于 api.domain.com。
[AllowAnonymous]
[HttpPost]
public HttpResponseMessage Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
Teacher t;
if (Teacher.Login(model.UserName,model.Password,out t))
{
// Roles.AddUserToRole(t.ID.ToString(), "teacher");
FormsAuthentication.SetAuthCookie(t.ID.ToString(), model.RememberMe);
FormsAuthenticationTicket ft = new FormsAuthenticationTicket(
1, // version
t.ID.ToString(), // get username from the form
DateTime.Now, // issue time is now
DateTime.Now.AddMinutes(30), // expires in 10 minutes
true, // cookie is not persistent
"teacher" // role assignment is stored
// in userData
);
string enTicket = FormsAuthentication.Encrypt(ft);
HttpResponseMessage res = Request.CreateResponse(HttpStatusCode.OK);
System.Net.Http.Headers.CookieHeaderValue cokie = new System.Net.Http.Headers.CookieHeaderValue(FormsAuthentication.FormsCookieName, enTicket);
cokie.Domain = "domain.com .domain.com";
IEnumerable<System.Net.Http.Headers.CookieHeaderValue> cookies = new System.Net.Http.Headers.CookieHeaderValue[] { cokie };
res.Headers.AddCookies(cookies);
return res;
}
return Request.CreateResponse(HttpStatusCode.Unauthorized);
}
// If we got this far, something failed, redisplay form
return Request.CreateResponse(HttpStatusCode.Unauthorized);
}
现在在 testing.domain.com,只需在 web.config 中根据其角色检查用户的身份验证。
<location path="TeacherPanel">
<system.web>
<authorization>
<allow roles="teacher" />
<deny users="?" />
</authorization>
</system.web>
</location>
这些是我发现通过这个问题进行研究的细节。随意提出任何建议,每条线索都会有所帮助。
testing.domain.com 的 Web 配置中的身份验证部分
<authentication mode="Forms">
<forms name=".Teacher" loginUrl="Login.aspx" protection="All" path="/" timeout="30" domain=".domain.com" />
</authentication>
api.domain.com 的 Web 配置中的身份验证部分
<authentication mode="Forms">
<forms name=".Teacher"
cookieless="UseCookies"
requireSSL="false"
domain=".domain.com"
/>
</authentication>
添加机器密钥并将过期时间添加到 Cookie 后
通过浏览器发布请求以进行身份验证:
POST /account/login HTTP/1.1
Host: api.domain.com
Connection: keep-alive
Content-Length: 123
Origin: http://testing.domain.com
User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
Content-Type: application/x-www-form-urlencoded
Accept: */*
Referer: http://testing.domain.com/TeacherLogin.aspx?ReturnUrl=%2fteacherpanel
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
__VIEWSTATE:/wEPDwUKMTY1NDU2MTA1MmRkCjMwS9YdCRDD3Qsd4GYnLj+tGGg=
UserName:username
Password:pass
服务器发送的响应:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/7.5
Set-Cookie: .Teacher=9C8FD43FABDC1817D21272361CBF798BB64C364DECC62E2F5F666D1B2A248076C67737D89B03F515D4D81524345584B11E206B2DDEAD5AA846A15BC17B32D86DF1C95A2943766AB5955C99A8DD0D5984089131838E158A90241B60D40A2D928086486E2BA3DDE95814E7FA303845FBDE235D69F54B6891852A09A80F7465FF8C59957A4D; expires=Wed, 26 Sep 2012 10:02:46 GMT; max-age=432000; domain=.domain.com; path=/; secure; httponly
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type
Date: Tue, 25 Sep 2012 10:02:46 GMT
Content-Length: 0
接下来获取发送到 testing.domain.com 的请求以进行测试(由于浏览器未将 cookie 发送回服务器而失败):
GET / HTTP/1.1
Host: testing.domain.com
Connection: keep-alive
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
Accept: */*
Referer: http://testing.domain.com/TeacherLogin.aspx?ReturnUrl=%2fteacherpanel
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
猜猜为什么浏览器不发回 Cookie?