如何从Oracle创建OracleMembershipProvider用户?

时间:2012-06-22 13:51:23

标签: asp.net oracle plsql asp.net-membership

我在Oracle11g和ASP.NET 4.0中使用OracleMembershipProvider。从ASP.NET端创建用户很简单,但我不太确定如何从Oracle存储过程中执行此操作。

我找到了ora_aspnet_Mem_CreateUser功能。但是,它被包装了,我找不到任何文档,所以我不确定所有参数是什么。此外,当您从ASP.NET调用CreateUser时,会有一个out参数,其中包含“MembershipCreateStatus枚举值,指示用户是否已成功创建。” ora_aspnet_Mem_CreateUser的唯一输出参数是userid,我甚至可以访问状态值吗?

我确实找到this answer,这是我正在尝试做的,但在T-SQL中。我需要在PL-SQL中执行此操作,如果可能,我需要确定用户是否已成功创建。

1 个答案:

答案 0 :(得分:0)

使用Reflector我发现MembershipCreateStatus out参数是ASP.NET函数的一部分,所以如果你想在Oracle函数中有类似的东西,你必须自己编写。

除了哈希之外,我还在PL-SQL中工作,所以一位同事在Java中编写了散列函数:

create or replace and compile java source named javasha1
as
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.security.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class javasha1 { 
    public static java.lang.String EncodePassword(java.lang.String passPlain, java.lang.String saltBase64) 
        throws NoSuchAlgorithmException, UnsupportedEncodingException, IOException
    {
        BASE64Decoder decoder = new BASE64Decoder();
        BASE64Encoder encoder = new BASE64Encoder();

        byte[] pass = passPlain.getBytes("UTF-16LE");
        byte[] salt = decoder.decodeBuffer(saltBase64);
        byte[] joined = new byte[salt.length + pass.length];
        System.arraycopy(salt, 0, joined, 0, salt.length);
        System.arraycopy(pass, 0, joined, salt.length, pass.length);

        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        return encoder.encodeBuffer(sha.digest(joined)).replaceAll("\r|\n", "");
    }
} 


FUNCTION EncodePassword
(
  p_Password VARCHAR2,
  p_Salt     VARCHAR2
)
RETURN VARCHAR2 
AS LANGUAGE java Name 'javasha1.EncodePassword(java.lang.String, java.lang.String) return java.lang.String';


PROCEDURE CreateUser
(
  p_UserName          VARCHAR2,
  p_ClearTextPassword VARCHAR2,
  p_Email             VARCHAR2
)
AS
  v_ApplicationName VARCHAR2(256);
  v_EncodedPassword NVARCHAR2(128);
  v_Now             DATE;
  v_ReturnValue     NUMBER;
  v_Salt            NVARCHAR2(128);
  v_UserID          RAW(16);

  FUNCTION Base64Encode
  (
    p_Raw RAW
  ) RETURN VARCHAR2
  AS
  BEGIN
    RETURN utl_raw.cast_to_varchar2(utl_encode.base64_encode(p_Raw));
  END Base64Encode;

BEGIN  
  v_ApplicationName := 'YOUR_APPLICATION_NAME';
  v_Now             := sys_extract_utc(Systimestamp);
  v_Salt            := Base64Encode(sys_guid());
  v_EncodedPassword := EncodePassword(p_Password => p_ClearTextPassword, p_Salt => v_Salt);

  v_ReturnValue := ora_aspnet.ora_aspnet_mem_createuser(
    applicationname_  => v_ApplicationName,
    username_         => p_UserName,
    password_         => v_EncodedPassword,
    passwordsalt_     => v_Salt,
    email_            => p_Email,
    passwordquestion_ => null,
    passwordanswer_   => null,
    isapproved_       => 1, -- true
    currenttimeutc    => v_Now,
    createdate_       => v_Now,
    uniqueemail       => 1, -- true
    passwordformat_   => 1, -- 0 = 'Clear', 1 = 'Hashed', 2 = 'Encrypted'
    userid_           => v_UserID -- out parameter
  );

END CreateUser;