我知道很多帖子在這裏都是關於此主題的,但我閱讀了其中的大部分內容,並且我的網站無法正常工作。
起初我知道所有的子域my web配置應該在同一身份驗證和計算機密鑰在子域中使用身份驗證進行單次登錄
<authentication mode="Forms">
<forms loginUrl="reg.aspx"
protection="All"
timeout="30"
name=".ASPXFORMSAUTH"
path="/"
requireSSL="false"
domain=".exam.com"
defaultUrl="reg.aspx" cookieless="UseCookies" enableCrossAppRedirects="true"/>
</authentication>
<!-- I also test this without dot at first : domain="exam.com" -->
<machineKey validationKey="C50B....7C529AD3CABE" decryptionKey="8A9...B72F" validation="SHA1"/>
這個代碼是在兩個子域和主domain.And我把這些代碼在subdomain.exam.com在全球頁面從餅乾登錄如果用戶登錄之前:
protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
{
if (FormsAuthentication.CookiesSupported == true)
{
if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
{
try
{
//let us take out the username now
string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
//let us extract the roles from our own custom cookie
string roles = BaseFunctions
.GetUserRoles(username);
//Let us set the Pricipal with our user specific details
e.User = new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));
}
catch (Exception)
{
//somehting went wrong
}
}
}
}
這些代碼登錄頁面上寫上的cookie數據爲每個用戶
protected void Page_Load(object sender, EventArgs e)
{
if (Membership.ValidateUser(Login1.UserName, Login1.Password))
{
// sometimes used to persist user roles
string userData = string.Join("|", GetUserRoles(Login1.UserName));
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // ticket version
Login1.UserName, // authenticated username
DateTime.Now, // issueDate
DateTime.Now.AddMinutes(30), // expiryDate
true, // true to persist across browser sessions
userData, // can be used to store additional user data
FormsAuthentication.FormsCookiePath); // the path for the cookie
// Encrypt the ticket using the machine key
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
// Add the cookie to the request to save it
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
//cookie.HttpOnly = true;
cookie.Domain = "exam.com";// and i use this cookie.Domain = ".exam.com";
Response.Cookies.Add(cookie);
// Your redirect logic
//Response.Redirect(FormsAuthentication.GetRedirectUrl(Login1.UserName, true));
}
}
public static string GetUserRoles(string username)
{
DataTable result = null;
try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select roles from Users where username = @uname";
cmd.Parameters.Add(new SqlParameter("@uname", username));
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
result = new DataTable();
da.Fill(result);
}
if (result.Rows.Count == 1)
{
return result.Rows[0]["roles"].ToString().Trim();
}
}
}
}
catch (Exception ex)
{
//Pokemon exception handling
}
//user id not found, lets treat him as a guest
return "guest";
}
但它不起作用!!!!!!!!!我現在應該怎麼做?問題是什麼?
我已編輯您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –