चूंकि इस प्रक्रिया पर दस्तावेज़ बहुत अस्पष्ट और भ्रमित (या पुराना) है, इसलिए मैं यह सत्यापित करना चाहता था कि मैं इसे सही तरीके से कर रहा हूं और किसी भी चरण को याद नहीं कर रहा हूं।प्रपत्र प्रमाणीकरण समझ संदर्भ.user.identity
मैं एक सुरक्षित लॉगिन सिस्टम बनाने की कोशिश कर रहा हूं, जो ब्राउज़र-बंद होने पर समाप्त हो जाता है।
<asp:Button ID="LoginButton" runat="Server" OnClick="Login_Authenticate" Text="Sign in" />
अंदर Login_Authenticate मुझे क्या करना:
-
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" defaultUrl="Index.aspx" name=".ASPXFORMSAUTH" timeout="100" />
</authentication>
<authorization>
<allow users="?" />
</authorization>
<machineKey decryption="AES" validation="SHA1" validationKey.......... />
तो मैं और उपयोगकर्ता नाम/पासवर्ड पाठ बॉक्स के साथ एक लॉगिन प्रपत्र इस बटन है - मेरी web.config में मैं निम्नलिखित है निम्नलिखित:
protected void Login_Authenticate(object sender, EventArgs e){
string userName = UserName.Text;
string password = Password.Text;
bool Authenticated = false;
// Here's code that makes sure that Username and Password is CORRECT
if(AuthClass.Authenticate(userName, password)){
Authenticated = true;
}
// error checking does happen here.
if (Authenticated)
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(30), rememberUserName, String.Empty, FormsAuthentication.FormsCookiePath);
string encryptedCookie = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookie);
cookie.Expires = DateTime.Now.AddMinutes(30);
Response.Cookies.Add(cookie);
//FormsAuthentication.RedirectFromLoginPage(userName, false);
Response.Redirect("MainPage.aspx");
}
}
--- MasterPage.master.cs में मैं Page_Init में निम्नलिखित की जांच की है () ---
if (Context.User.Identity.IsAuthenticated)
{
int userid = (int)Session["userid"];
if (userid == null)
{
userid = GetUserID(Context.User.Identity.Name);
if (userid != null)
{
Session["userid"] = userid;
}
}
}
संपादित करें: --- Global.asax; कुछ कोड है कि मैं काफी यकीन नहीं कर रहा हूँ सही है या पता है कि यह
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
// look if any security information exists for this request
if (HttpContext.Current.User != null)
{
// see if this user is authenticated, any authenticated cookie (ticket) exists for this user
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
// see if the authentication is done using FormsAuthentication
if (HttpContext.Current.User.Identity is FormsIdentity)
{
// Get the roles stored for this request from the ticket
// get the identity of the user
FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
//Get the form authentication ticket of the user
FormsAuthenticationTicket ticket = identity.Ticket;
//Get the roles stored as UserData into ticket
string[] roles = { };
//Create general prrincipal and assign it to current request
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(identity, roles);
}
}
}
}
--- तब से हर पृष्ठ पर, मैं सत्र userid का उपयोग उपयोगकर्ता की जानकारी और सामग्री इकट्ठा करने और यह सुनिश्चित करने के लिए करता है उपयोगकर्ता के पास उचित प्रमाणीकरण और समूह-भूमिका अनुमतियां हैं।
क्या यह सब सही है? या मुझे कहीं और कुछ डिक्रिप्ट करना है?
क्या यह एक सुरक्षित उपयोगकर्ता लॉगिन बनाने के लिए पर्याप्त है? या क्या मुझे फॉर्म प्रमाणीकरण से परेशान नहीं होना चाहिए और अपनी खुद की कुकीज़ बनाने और इसे स्वयं प्रबंधित करने का अपना तरीका ढूंढना चाहिए?
क्या हुआ अगर कोड हमेशा प्रमाणीकृत विफल रहता है प्रमाणीकृत = false bool सच रिटर्न; // यहां कोड है जो सुनिश्चित करता है कि उपयोगकर्ता नाम और पासवर्ड सही प्रमाणीकृत = सत्य है; यह आपके पास शीर्ष पर है .. लेकिन यदि कोड विफल रहता है तो यह कभी भी प्रमाणीकृत को गलत पर सेट नहीं करता है .. – MethodMan
@DJKRAZE कोड त्रुटियों पर गलत होने पर सेट करता है। मैंने उपयोगकर्ता नाम/पासवर्ड की जांच के लिए अभी तक लंबा कोड शामिल नहीं किया है। प्रमाणीकृत = सत्य केवल तभी होता है जब सब कुछ सफल हो। – Dexter
स्पष्टीकरण के लिए कुछ और कोड जोड़ा गया है और मेरे ग्लोबल.एक्सएक्स कोड का उपयोग कर रहा हूं। मुझे नहीं पता कि यह काम कर रहा है और मैं बहुत उलझन में हूं। – Dexter