我如何表现我的应用程序登录与登录的asp.net相同 [英] How do I behave my application's logged in same as asp.net logged in

查看:69
本文介绍了我如何表现我的应用程序登录与登录的asp.net相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

通过阅读我的问题,你会得到这个想法。

我正在开发需要用户登录信息才能访问会员的应用程序资源/页。我的问题是,当我从数据库验证用户是有效用户时,如何以满足以下条件的方式注册他:



Request.IsAuthenticated

'用户是否经过身份验证以查看所请求的页面?



我的意思是,如果我们在VS 2010中创建一个新项目,它会创建一个数据库,一个帐户文件夹(某些成员只有页面),当我们使用默认设置注册时,它会自动登录我们并仅允许访问成员页面。

我们如何使用自己的自定义设置实现此行为?

任何建议任何事情都会非常有帮助。

提前感谢。



(PS:请问在向这个问题报告错误问题之前我请!)

Hello everyone!
By reading my question, you would get the idea.
I'm working on application that requires user login information to access to members-only resources/pages. My question is, when I verified from database that user is valid user, how can I register him in a way that satisfy the following:

Request.IsAuthenticated
'is user is authenticated to view that requested page or not?

I mean, if we create a new project in VS 2010, it creates a database, an account folder( some members only page), when we register using default setting, it automatically signed us in and give access to members only pages.
how can we implement this behavior using my own custom settings?
Any suggestion anything will be very very helpful.
thanks in advance.

(P.S: please ask me before reporting 'bad question' to this question. please!)

推荐答案

您可以添加从RoleProvider继承的自定义角色提供程序类并覆盖这些函数。添加类后,在Web配置中更改默认提供程序名称



例如:

将一个名为CustomRoleProvider的类添加到Appcode文件夹中



使用System;

使用System.Collections.Specialized;

使用System.Web.Security; < br $>


命名空间TestApp.AppCode

{

公共类CustomRoleProvider:RoleProvider

{



公共覆盖字符串ApplicationName {get;组; }







public override void Initialize(string name,NameValueCollection config)

{

base.Initialize(name,config);



}

public override bool IsUserInRole( string username,string roleName)

{

string [] roles = GetRolesForUser(username);

foreach(角色中的字符串角色)

{

if(role.Equals(roleName,StringComparison.OrdinalIgnoreCase))

{

return true;

}

}

返回false;

}



public override string [] GetRolesForUser(string username)

{

//添加代码以获取用户角色

}



pu blic override void AddUsersToRoles(string [] usernames,string [] roleNames){



}



public override void CreateRole(string roleName){}

public override bool DeleteRole(string roleName,bool throwOnPopulatedRole){return false; } $ / $
public override string [] FindUsersInRole(string roleName,string usernameToMatch){return null; } $ / $
public override string [] GetAllRoles(){return null; } $ / $
public override string [] GetUsersInRole(string roleName){return null; } $ / $
public override void RemoveUsersFromRoles(string [] usernames,string [] roleNames){}

public override bool RoleExists(string roleName){return false; }



}

}

}



更改您的Web配置角色提供程序

You can add a custom Role Provider class inherited from RoleProvider and override the functions. After adding the class, change your default provider name in the web config

For Example:
Add a class named CustomRoleProvider to you Appcode folder

using System;
using System.Collections.Specialized;
using System.Web.Security;

namespace TestApp.AppCode
{
public class CustomRoleProvider : RoleProvider
{

public override string ApplicationName { get; set; }



public override void Initialize(string name, NameValueCollection config)
{
base.Initialize(name, config);

}
public override bool IsUserInRole(string username, string roleName)
{
string[] roles = GetRolesForUser(username);
foreach (string role in roles)
{
if (role.Equals(roleName, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}

public override string[] GetRolesForUser(string username)
{
//Add code to get user roles
}

public override void AddUsersToRoles(string[] usernames, string[] roleNames){

}

public override void CreateRole(string roleName) { }
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { return false; }
public override string[] FindUsersInRole(string roleName, string usernameToMatch) { return null; }
public override string[] GetAllRoles() { return null; }
public override string[] GetUsersInRole(string roleName) { return null; }
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { }
public override bool RoleExists(string roleName) { return false; }

}
}
}

Change your web config role provider
<roleManager enabled="true" defaultProvider="CustomRoleProvider" >
     <providers>
       <clear/>
       <add name="CustomRoleProvider" type="TestApp.AppCode.CustomRoleProvider"/>
     </providers>
   </roleManager>


这篇关于我如何表现我的应用程序登录与登录的asp.net相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆