您好我正在使用FormsAuthentication.RedirectFromLoginPage用戶登錄和重定向到default.aspx頁面。 我想如果一個用戶叫做管理登錄被重定向到頁面admin.aspxFormsAuthentication.RedirectFromLoginPage到自定義頁面
這可能嗎?
您好我正在使用FormsAuthentication.RedirectFromLoginPage用戶登錄和重定向到default.aspx頁面。 我想如果一個用戶叫做管理登錄被重定向到頁面admin.aspxFormsAuthentication.RedirectFromLoginPage到自定義頁面
這可能嗎?
試試這個,我認爲這是你會用一個簡單的解決方案中獲得最接近:
FormsAuthentication.SetAuthCookie(username, true);
Response.Redirect("mypage.aspx");
如果您使用的是ASP.NET的MembershipProvider登錄控制,你可以寫你的邏輯在LoggedIn event
<asp:Login id="Login1" runat="server" OnLoggedIn="OnLoggedIn"></asp:Login>
protecetd void OnLoggedIn(object sender, EventArgs e)
{
if(Roles.IsUserInRole(User.Identity.Name, "Administrators"))
{
//Redirect to admin page
Response.Redirect("~/Admin.aspx");
}
}
不要忘了把一些保護上admin.aspx頁藏漢,櫃面有人類型直接在url中
默認行爲是重定向到最初請求的資源,所以如果用戶試圖訪問'admin.aspx'並且沒有通過身份驗證,用戶將被髮送到登錄頁面。成功進行身份驗證後,用戶將被髮送到最初請求的URL(admin.aspx)。
用戶 - >「admin.aspx」 - > NOAUTH - >登錄 - >「admin.aspx」
因此,而不是手動試圖發送至某處的用戶,使用這種默認行爲不會爲你工作?默認行爲實際上是「健壯的」(可以是「admin2.aspx」,「admin3.aspx」等等......您可以擁有任意數量的「受保護資源」,並且內置過程處理所有這些資源。 ..)
驗證用戶
假設你已經通過我以前的上述文章了,你有一個登錄頁面。現在當用戶單擊登錄按鈕Authenticate
方法觸發時,讓我們看看該方法的代碼。
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string userName = Login1.UserName;
string password = Login1.Password;
bool rememberUserName = Login1.RememberMeSet;
// for this demo purpose, I am storing user details into xml file
string dataPath = Server.MapPath("~/App_Data/UserInformation.xml");
DataSet dSet = new DataSet();
dSet.ReadXml(dataPath);
DataRow[] rows = dSet.Tables[0].Select(" UserName = '" + userName + "' AND Password = '" + password + "'");
// record validated
if (rows.Length > 0)
{
// get the role now
string roles = rows[0]["Roles"].ToString();
// Create forms authentication ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version
userName, // Username to be associated with this ticket
DateTime.Now, // Date/time ticket was issued
DateTime.Now.AddMinutes(50), // Date and time the cookie will expire
rememberUserName, // if user has chcked rememebr me then create persistent cookie
roles, // store the user data, in this case roles of the user
FormsAuthentication.FormsCookiePath); // Cookie path specified in the web.config file in <Forms> tag if any.
// To give more security it is suggested to hash it
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies); // Hashed ticket
// Add the cookie to the response, user browser
Response.Cookies.Add(cookie); // Get the requested page from the url
string returnUrl = Request.QueryString["ReturnUrl"];
// check if it exists, if not then redirect to default page
if (returnUrl == null) returnUrl = "~/Default.aspx";
Response.Redirect(returnUrl);
}
else // wrong username and password
{
// do nothing, Login control will automatically show the failure message
// if you are not using Login control, show the failure message explicitely
}
}
您可以通過放置核心角色名稱或從數據庫獲取用戶捲來檢查它。我已經修改了這個爲我的實體框架。
TestEntities entities = new TestEntities();
var user = (from s in entities.UserTables
where s.UserName == loginControl.UserName
&& s.Password == loginControl.Password
select s).SingleOrDefault();
並放置在用戶角色:
user.Role
沿着這條你必須做在Global.asax一些更改文件 到目前爲止我們已經設定所需的詳細信息,甚至用戶的窗體身份驗證票現在如何在每個請求中檢索這些信息,並發現請求來自哪種角色類型?爲此,我們需要使用Global.asx文件的Application_AuthenticateRequest事件。請參閱下面的代碼。
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 forms authetication ticket of the user
FormsAuthenticationTicket ticket = identity.Ticket;
// get the roles stored as UserData into the ticket
string[] roles = ticket.UserData.Split(',');
// create generic principal and assign it to the current request
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(identity, roles);
}
}
}
}
在這個連,檢查是否存在用戶,他/她的身份驗證和TH用戶的identy類型是FormsIdentity,我得到了用戶的當前身份和獲得我已經設定在票後Authentia的時間。一旦我獲得了認證票證,我只需從票證中獲取UserData並將其分割爲角色(請記住,我們已將角色存儲爲逗號分隔值)。現在,我們擁有當前的用戶角色,因此我們可以將當前用戶的角色與當前身份一起傳遞給GenericPrincipal對象,並將其分配給curent用戶對象。這將使我們能夠使用IsInRole方法來檢查特定用戶是否屬於特定角色。
如何檢查用戶是否具有特定角色?
要檢查用戶是否屬於某個特定角色,請使用以下代碼。如果當前記錄來自經過身份驗證並具有管理角色的用戶,則此代碼將返回true。
HttpContext.Current.User.IsInRole("admin")
如何檢查用戶是否通過身份驗證?
要檢查用戶是否已通過身份驗證,請使用以下代碼。
HttpContext.Current.User.Identity.IsAuthenticated
要獲得經過驗證的用戶
HttpContext.Current.User.Identity.Name
的用戶名密碼記住的..這代碼需要在表格標記一些webconfig設置的事情:以下身份驗證設置到您的網頁
添加。配置文件下。
<authentication mode="Forms">
<forms defaultUrl="default.aspx" loginUrl="~/login.aspx" slidingExpiration="true" timeout="20" ></forms>
</authentication>
對於每一個用戶,如果你想確保特定的文件夾,您可以將設置爲他們無論是在父母的web.config文件(根文件夾),或該文件夾的web.config文件。
指定在根web.config文件(用於管理在這種情況下)的文件夾角色設置
<location path="Admin">
<system.web>
<authorization>
<allow roles="admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
編寫代碼之外,但在標籤中根的web.config文件。在這裏,我指定如果路徑包含Admin文件夾的名稱,則只允許具有「admin」角色的用戶,並且所有其他用戶都被拒絕。
指定在文件夾特定web.config文件的文件夾的作用的設置(在這種情況下爲用戶)
<system.web>
<authorization>
<allow roles="User"/>
<deny users="*"/>
</authorization>
</system.web>
寫入此代碼到web.config文件的用戶文件夾。您可以在root的web.config文件中爲用戶指定設置,就像我爲上面的Admin所做的那樣。這只是指定設置的另一種方式。這個設置應該放在標籤下。
指定設置身份驗證的用戶
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
寫代碼到安全的文件夾的web.config文件。這是指定所有匿名用戶都被拒絕訪問此文件夾,並且只有經過身份驗證的用戶才能被允許,而不管他們的角色如何。
希望這會給你一點想法解決你的問題。它對我來說工作得很好。希望你也能解決你的問題。