PDA

View Full Version : سلام.میشه یه کمکی به من کنین راجب لوگین در ساب دامین ها



haniyehghassami
شنبه 05 بهمن 1392, 08:43 صبح
سلام
وقتتون بخیر

مشکلم اینه که چجوری لوگین کنم در ساب دامین و در تمام دامین کاربرم authenticate باشه؟

راه حل: استفاده از کوکی هست که با سرچ کردن پیداش کردم . یعنی باید کاربر که لوگین میکنه اطلاعاتش بره تو کوکی نگه داری بشه.من این تابع رو تو login1_loginig نوشتم که تو کوکی این اطلاعات ذخیره بشه:


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 = ".example.ir";
Response.Cookies.Add(cookie);

// Your redirect logic
//Response.Redirect(FormsAuthentication.GetRedirectU rl(Login1.UserName, true));
}



////////////////////////////////////////////////////////////////////////////////////////////////////////////

public static string GetUserRoles(string username)
{
DataTable result = null;
try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs[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";
}



این کد را تو ساب دامین مخصوص لوگینم نوشتم. و تو وب کانفیگم هم اینو زدم(میدونم که باید این قطعه کدم تو تمام وب کانفیگ های دامنه اصلی و ساب دامین هام کپی کنم)



<authentication mode="Forms"> <forms loginUrl="reg.aspx"
protection="All"
timeout="30"
name=".ASPXFORMSAUTH"
path="/"
requireSSL="false"
domain=".example.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"/>

و توی گلوبال ساب دامینم هم اینو زدم:


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
}


}
}

}



همین.

اما کار نمی کنه

یادداشت : در وب کانفیگم این تغییرات ایجاد کردم
1- حذف لوگین پیج
2-حذف یوزکوکی

زمانی که ساب دامین لوگین باز میشه من تو کوکی هام دوتا کوکی دارم یکی ASP.NET_SessionID و دیگری به همون نامی که تو وب کانفیگ دادم. نمیدونم مشکل از کجاست که 2 تا کوکی میاره